【发布时间】:2013-10-17 02:09:11
【问题描述】:
这可能是一个基本问题,但我正在按照本教程进行操作,有时代码看起来像这样。
<?php
class person
{
public $name;
public $height;
protected $social_security_no;
private $pin_number = 3242;
public function __construct($person_name)
{
$this->name = $person_name;
}
public 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
{
public 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();
?>
我收到这个错误
致命错误:调用受保护的方法employee::get_name() from C:\Users\danny\Documents\Workspace\test\index.php 中的上下文'' 第 13 行
问题似乎是当我在员工类的 get_name() 函数中添加 protected 时,但在我看来,这是本教程中覆盖的首选方法。有什么想法吗?
【问题讨论】:
-
受保护的方法不能在类外调用