当前位置:网站首页>php中self和static在方法中的区别
php中self和static在方法中的区别
2022-07-02 07:28:00 【kankan231】
先上一段代码
class Father{
public static $a = 2;
public function selfTest(){
var_dump(self::$a);
}
public function staticTest(){
var_dump(static::$a);
}
}
class Son extends Father {
public static $a = 20;
}
$obj = new Son();
$obj->selfTest();
$obj->staticTest();输出:
int(2)
int(20)
结论:self表示self关键字所在的类,也就是说这里的self是Father类,self::$a就是Father::$a,尽管是子类调用selfTest方法self::$a也不会表现出多态性
static表示调用该方法的类,这里obj对象的类是Son,则static::$a表示的就是Son::$a,如果obj对象的类是Father类,则static::$a表示的就是Father::$a,也就是说static::$a会表现出多态性
边栏推荐
- What are the popular frameworks for swoole in 2022?
- Oracle 笔记
- 13. Semaphore critical zone protection
- Shapiro Wilk normal analysis by SPSS
- 二叉树专题--AcWing 47. 二叉树中和为某一值的路径(前序遍历)
- 二叉树专题--【深基16.例7】普通二叉树(简化版)(multiset 求前驱 后继 哨兵法)
- 1287_FreeRTOS中prvTaskIsTaskSuspended()接口实现分析
- 华为应用市场应用统计数据问题大揭秘
- PCL 从一个点云中提取一个子集
- MySQL数据库远程访问权限设置
猜你喜欢

二叉树专题--AcWing 3384. 二叉树遍历(已知先序遍历 边建树 边输出中序遍历)

MySQL environment configuration

Hdu1234 door opener and door closer (water question)

UVM learning - object attribute of UVM phase

axis设备的rtsp setup头中的url不能带参

全网显示 IP 归属地,是怎么实现的?

华为快应用中如何实现同时传递事件对象和自定义参数

拆解美图SaaS:开着飞机换引擎

首份中国企业敏捷实践白皮书发布| 附完整下载

华为AppLinking中统一链接的创建和使用
随机推荐
Kustomize使用手册
UVM - usage of common TLM port
V2X-Sim数据集(上海交大&纽约大学)
HDU1236 排名(结构体排序)
二叉树专题--AcWing 19. 二叉树的下一个节点(找树中节点的后继)
Common methods of JS array
洛谷 P3398 仓鼠找 sugar(树上倍增 lca 判断树中两条路径是否相交 结论)
13. Semaphore critical zone protection
12.进程同步与信号量
MySQL lethal serial question 4 -- are you familiar with MySQL logs?
Open the encrypted SQLite method with sqlcipher
【快应用】text组件里的文字很多,旁边的div样式会被拉伸如何解决
MongoDB 学习整理(条件操作符,$type 操作符,limit()方法,skip() 方法 和 sort() 方法)
Nodejs+express+mysql simple blog building
Mongodb quickly get started with some simple operations of mongodb command line
JSP webshell free -- webshell free
UVM learning - object attribute of UVM phase
OpenMLDB Meetup No.4 会议纪要
力扣(LeetCode)182. 查找重复的电子邮箱(2022.07.01)
PCL eigen introduction and simple use