您好,欢迎访问一九零五行业门户网

PHP中new static与new self的区别

php中new static()与new self()的区别
self:始终指向self代码所在类的本身,无论这个类被继承了多少次,self都指向最初使用self的类;
static:指向使用static的类,只有通过继承后,才能体现出static存在的意义,否则static和self一样。
classa {publicfunctiongetstatic(){returnnewstatic(); } publicfunctiongetself(){returnnewself(); }}classbextendsa{}var_dump((new b())->getself());//avar_dump((new b())->getstatic());//b
疑问:
1、不知道new static()和new self()有什么用;
2、如果要使用new self(),为何不使用new a();
以上就介绍了php中new static与new self的区别,包括了方面的内容,希望对php教程有兴趣的朋友有所帮助。
其它类似信息

推荐信息