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_static()); // A
self是php5.3中加进来的
以下是低版本static的取代方法
class A { public function create1() { $class = get_class($this); return new $class(); } public function create2() { return new static(); } } class B extends A { } $b = new B(); var_dump(get_class($b->create1()), get_class($b->create2())); /* The result string(1) "B" string(1) "B" */