class A {
public static function get_self() {
return new self();
}
public static function get_static() {
return new static();
}
}
class B extends A {
public function testB(){
return 'testB';
}
}
echo get_class(A::get_self()); // A
echo get_class(A::get_static()); // A
echo get_class(B::get_self()); // A
echo get_class(B::get_static()); // B
#echo B::get_self()->testB(); // Fatal error: Uncaught Error: Call to undefined method A::testB()
echo B::get_static()->testB(); // testB
new self() => 生成的物件為實際寫有這句 code 的 classnew static() =>生成的物件為呼叫使用這句 code 的 class
參考:
php - New self vs. new static - Stack Overflow
class - What does new self(); mean in PHP? - Stack Overflow
沒有留言:
張貼留言