我剛剛與OOP PHP握手,雖然我已經理解了它,但我在final
關鍵字中遇到了一些問題。本書說,Final
停止子類方法,覆蓋超類方法。下面我試過了,我的IDE顯示了一些錯誤,但它不起作用。我看了看,但書中說這應該啓用超類方法而不是子類。爲最終方法定義子類方法時的錯誤
$object=new userprofile();
$object->name="Mike";
$object->age=22;
$object->sex="Male";
//2 properties of subclass
$object->email="[email protected]";
$object->website="http://domain.com";
echo $object->get_name(); //method call
class user{
public $name, $age, $sex;
final function get_name(){
return "Not overriden";
}
}
class userprofile extends user{
public $email,$website;
function get_name(){
return $this->website;
}
}
如果你運行腳本,你會得到什麼錯誤? – JJJ 2012-03-27 17:07:54
嗨Juhana ..致命錯誤:無法覆蓋最後一個方法用戶:: get_name()在H:\ xampp \ htdocs \ PHP_testers \ objects_OOP \ object_inheritence.php在第41行 – 2012-03-27 17:09:23