当前位置:网站首页>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会表现出多态性
边栏推荐
猜你喜欢

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

【TS】1368- 秒懂 TypeScript 泛型工具类型!

Retrofit's callback hell is really vulnerable in kotlin synergy mode!

一招快速实现自定义快应用titlebar

JSP webshell free -- webshell free

Beautiful and intelligent, Haval H6 supreme+ makes Yuanxiao travel safer

Mysql database remote access permission settings

华为快应用中如何实现同时传递事件对象和自定义参数
![2. Hacking lab script off [detailed writeup]](/img/f3/29745761cd5ad4df84c78ac904ea51.png)
2. Hacking lab script off [detailed writeup]

13. Semaphore critical zone protection
随机推荐
PCL extracts a subset from a point cloud
PCL之K-d树与八叉树
【AGC】如何解决事件分析数据本地和AGC面板中显示不一致的问题?
Easyexcel, a concise, fast and memory saving excel processing tool
Operator-1 first acquaintance with operator
12. Process synchronization and semaphore
大华设备播放过程中设置播放速度
Thanos Receiver
MongoDB 学习整理(条件操作符,$type 操作符,limit()方法,skip() 方法 和 sort() 方法)
1287_ Implementation analysis of prvtaskistasksuspended() interface in FreeRTOS
LabVIEW为什么浮点数会丢失精度
QT学习日记8——资源文件添加
UWA报告使用小技巧,你get了吗?(第四弹)
洛谷 P1892 [BOI2003]团伙(并查集变种 反集)
MySQL lethal serial question 4 -- are you familiar with MySQL logs?
二叉树专题--P1030 [NOIP2001 普及组] 求先序排列
华为游戏初始化init失败,返回错误码907135000
Filtering of PCL
Session cookies and tokens
MySQL environment configuration