调用父类中不存在的方法,自动调用子类中的方法!

浏览:915 发布日期:2016/04/27 分类:技术分享 关键字: 父类中不存在的方法 自动调用子类中的方法
<?php
class Person{
private $name;
private $sex;
private $age;
private $info;
public $ChildClass_arr=[];

public function __construct($name=null,$sex=null,$age=null){
$this->ChildClass_arr=include "./ChildClass_arr.php";
$this->name=$name;
$this->sex=$sex;
$this->age=$age;
}

public function info(){
echo $this->name."<br/>";
echo $this->sex."<br/>";
echo $this->age."<br/>";
}

public function __set($name,$value){
$this->$name=$value;
}

public function __get($name){
return $this->$name."aaa";
}

public function __call($name,$args){
echo "<b>Sorry the class of ".get_class($this)." has no this function name:{$name}<br/>and It will auto to use ChildClass's Method if it has!<br></b> ";
foreach($this->ChildClass_arr as $value){

$methods_arr=get_class_methods($value);
foreach($methods_arr as $value1){
if($name===$value1){
$c=new $value();
$c->$name($args[0]);
}else{
echo "Warning:ChildClass also has no this funtion !<br>";
exit;
}
}


}


}
}

class Student extends Person{

public function sayHello($str){
echo "Hello {$str}!<br/>";
}
}

$p=new Person("zhangsan","nan",33);
$p->sayHello1("This is a Test!");
$p->info();
提示:创建一个注册文件,有子类就注册进去。
ChildClass_arr.php
<?php
return ["Student",];
?>
最佳答案
评论( 相关
后面还有条评论,点击查看>>