<?php
class Person{
protected $_name;
protected $_age;
public function __construct($name,$age){
$this->_name = $name;
$this->_age = $age;
}
public function __isset($property_name){
return isset($property_name);
}
public function __set($property_name,$value){
$this->$property_name = $value;
}
public function __get($property_name){
if(isset($property_name)){
return $this->$property_name; //第33行
}else{
return NULL;
}
}
}
class Student extends Person{
private $_school;
public function study(){
echo $this->_name."在学习";
}
}
$student1 = new Student("张三",20);
$student1->_school = 'XX大学';
echo "姓名:".$student1->_name."<br>";
echo "年龄:".$student1->_age."<br>";
echo "学校:".$student1->_school."<br>";
?>
为什么提示第33行有错误, 未定义的$Student::$_school。
但是,如果把Student子类里的private $_school;改为protected $_school;则不再会有错误提示。子类明明继承了魔术方法,为什么在外部设置私有属性还是不可以呢?
晕,迷惑了……
class Person{
protected $_name;
protected $_age;
public function __construct($name,$age){
$this->_name = $name;
$this->_age = $age;
}
public function __isset($property_name){
return isset($property_name);
}
public function __set($property_name,$value){
$this->$property_name = $value;
}
public function __get($property_name){
if(isset($property_name)){
return $this->$property_name; //第33行
}else{
return NULL;
}
}
}
class Student extends Person{
private $_school;
public function study(){
echo $this->_name."在学习";
}
}
$student1 = new Student("张三",20);
$student1->_school = 'XX大学';
echo "姓名:".$student1->_name."<br>";
echo "年龄:".$student1->_age."<br>";
echo "学校:".$student1->_school."<br>";
?>
为什么提示第33行有错误, 未定义的$Student::$_school。
但是,如果把Student子类里的private $_school;改为protected $_school;则不再会有错误提示。子类明明继承了魔术方法,为什么在外部设置私有属性还是不可以呢?
晕,迷惑了……