当前位置:网站首页>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会表现出多态性
边栏推荐
猜你喜欢
随机推荐
6种单例模式的实现方式
12. Process synchronization and semaphore
Hdu1228 a + B (map mapping)
PCL projection point cloud
正则及常用公式
UWA report uses tips. Did you get it? (the fourth bullet)
【AppLinking实战案例】通过AppLinking分享应用内图片
UVM factory mechanism
华为联机对战服务玩家掉线重连案例总结
4. Random variables
618再次霸榜的秘密何在?耐克最新财报给出答案
"Matching" is true love, a new attitude for young people to make friends
MYSQL关键字
Nodejs+express+mysql simple blog building
Shapiro Wilk normal analysis by SPSS
2022-06-17
Start class, data analysis, high salary training plan, elite class
点云投影图片
华为游戏初始化init失败,返回错误码907135000
Learn open62541 -- [66] UA_ Generation method of string