PHP中new static()与new self()的区别!
self:始终指向self代码所在的类
的本身,无论这个类被继承了多少次,self都指向初使用self的类
static:指向使用static的类
,只有通过继承后,才能体现出static的意义,否则static和self一样
class A {
public static function get_self() {
return new self();
}
public static function get_static() {
return new static();
}
}
class B extends A {}
echo get_class(B::get_self()); // A
echo get_class(B::get_static()); // B
echo get_class(A::get_self()); // A
echo get_class(A::get_static()); // A
stackoverflow:New self vs. new static
最后更新于 2017-02-14 11:11:01 并被添加「」标签,已有 323 位童鞋阅读过。
本站使用「署名 4.0 国际」创作共享协议,可自由转载、引用,但需署名作者且注明文章出处
此处评论已关闭