这可能是一个基本的问题,但即时通讯本教程后,在一个点上代码看起来像这样。为什么我不能在PHP中重写这个受保护的函数?
<?php
class person {
var $name;
public $height;
protected $social_security_no;
private $pin_number = 3242;
function __construct($person_name){
$this->name = $person_name;
}
function set_name($new_name){
$this->name = $new_name;
}
protected function get_name() {
return $this->name;
}
public function get_pin_number_public() {
$this->pub_pin = $this->get_pin_number();
return $this->pub_pin;
}
private function get_pin_number() {
return $this->pin_number;
}
}
class employee extends person {
function __construct($person_name){
$this->name = $person_name;
}
protected function get_name() {
return $this->name;
}
}
然而,当我使用这个
<?php include("class_lib.php"); ?>
</head>
<body id="theBody">
<div>
<?php
$maria = new person("Default");
$dave = new employee("David Knowler");
echo $dave->get_name();
?>
我得到这个错误
Fatal error: Call to protected method employee::get_name() from context '' in C:\Users\danny\Documents\Workspace\test\index.php on line 13
的问题似乎是当我添加受保护的GET_NAME()函数在雇员类中,但在我看来,这是在教程中重写的首选方式。有任何想法吗?
保护的方法不能被称为类 –