new self() 与 new static() 用法区别
new static() 是php 5.3添加的延迟静态绑定(后期延迟绑定)功能。
它和 new self() 的相同点在于都是用来实例化一个类,
但 new self() 是实例化代码声明时所在的类,
而 new static() 是实例化调用时所在的类。
事例:
class A
{
public static function get_self()
{
return new self();
}
public static function get_static()
{
return new static();
}
}
class B extends A
{
};
var_dump(A::get_self());
var_dump(A::get_static());
var_dump(B::get_self());
var_dump(B::get_static());
{
public static function get_self()
{
return new self();
}
public static function get_static()
{
return new static();
}
}
class B extends A
{
};
var_dump(A::get_self());
var_dump(A::get_static());
var_dump(B::get_self());
var_dump(B::get_static());
输出:
class A#1 (0) {}
class A#1 (0) {}
class A#1 (0) {}
class B#1 (0) {}