【发布时间】:2016-07-28 02:56:01
【问题描述】:
当我尝试从扩展类而不是基类设置属性的值时,它不应该产生错误吗?
<?php
class first{
public $id = 22;
private $name;
protected $email;
public function __construct(){
echo "Base function constructor<br />";
}
public function printit(){
echo "Hello World<br />";
}
public function __destruct(){
echo "Base function destructor!<br />";
}
}
class second extends first{
public function __construct($myName, $myEmail){
$this->name = $myName;
$this->email = $myEmail;
$this->reveal();
}
public function reveal(){
echo $this->name.'<br />';
echo $this->email.'<br />';
}
}
$object = new second('sth','aaa@bbb.com');
?>
【问题讨论】:
-
不行,它不能访问父类中的私有属性(甚至不知道它存在),所以它在扩展类中创建了一个新的公共属性
-
只做
var_dump的$object我想你会有答案
标签: php constructor private access-specifier