当前位置:网站首页>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会表现出多态性
边栏推荐
- JSP webshell免殺——JSP的基礎
- Kustomize user manual
- Learn open62541 -- [66] UA_ Generation method of string
- MongoDB 学习整理(条件操作符,$type 操作符,limit()方法,skip() 方法 和 sort() 方法)
- 【AppLinking实战案例】通过AppLinking分享应用内图片
- 计算序列之和
- PCL之滤波
- Start class, data analysis, high salary training plan, elite class
- In the face of uncertainty, the role of supply chain
- Read H264 parameters from mediarecord recording
猜你喜欢
随机推荐
618再次霸榜的秘密何在?耐克最新财报给出答案
【AGC】构建服务3-认证服务示例
二叉树专题--AcWing 1497. 树的遍历(利用后、中序遍历,构建二叉树)
PCL 点云转深度图像
How to get the password of cpolar?
Start class, data analysis, high salary training plan, elite class
AppGallery Connect场景化开发实战—图片存储分享
JSP webshell免杀——webshell免杀
Oracle 笔记
14. Code implementation of semaphore
Pywin32 opens the specified window
【快应用】text组件里的文字很多,旁边的div样式会被拉伸如何解决
js数组常用方法
MYSQL关键字
MySQL keyword
Record attributeerror: 'nonetype' object has no attribute 'nextcall‘
JS settimeout() and interview questions
使用华为性能管理服务,按需配置采样率
Oracle notes
华为快应用中如何实现同时传递事件对象和自定义参数









