@php中重载类的实践一
<?php
header("Content-type:text/html;charset=utf-8"); //设置输出编码格式
class Test{ //创建一个类
protected $name;
protected $age;
function __construct($name,$age){
$this->name=$name;
$this->age=$age;
}
public function __call($functionName,$agrument){ //调用不存在的方法会自动调用魔术方法__call,并可以把方法\参数传进来显示
echo "你调用的函数名称".$functionName."不存在!<br>".print_r($agrument)."参数不存在!";
}
public function func(){
echo "test";
}
public function get(){
echo "我的名字是{$this->name},我的年龄是{$this->age}";
}
}
$a = new Test('李某某','18');
$a->func();
echo "<br>";
$a->logo('123','1234','abc');
echo "<br>";
$a->get();
echo "<br>";
class Test1 extends Test{
protected $school;
function __construct($name,$age,$school){
parent::__construct($name,$age); //重载父类的构造函数
$this->school = $school; //增加一个成员属性
}
public function get(){
parent::get(); //重载父类方法
echo "我在{$this->school}学校读书";
}
}
$b=new test1('李某某','18','北京清华大学'); //实例类
echo $b->get(); //调用方法