我試圖寫下面的代碼在PHP要求作出澄清關於PHP OOP程序
class A {
protected static $comment = "I am A" ;
public static function getComment() {
return self :: $comment;
}
}
class B extends A {
protected static $comment = "I am B" ;
}
echo B::getComment() ; // echoes "I am A"
難道不應該返回I am B
?在oop中PHP不會覆蓋父項嗎?感謝您的澄清。
== ==編輯
而且我的問題是有什麼區別,然後靜態和實例之間,因爲例如它的工作原理:
class A {
protected $comment = "I am A" ;
public function getComment() {
return $this -> comment ;
}
}
class B extends A {
protected $comment = "I am B" ;
}
$B=new B ;
echo $B->getComment();
看看這篇文章: [http://stackoverflow.com/questions/4280001/protected-static-member-variables] [1] [1]:http://stackoverflow.com/questions/4280001/protected-static-member-va riables – 2013-02-28 11:50:59
太棒了!我不知道那個晚期的靜態綁定的東西 - 這真的很有用:) – user28490 2013-02-28 11:56:31