// 创建父类 class Person { private $id; public function GetId() { return $this->id; } public function SetId($id) { $this->id = $id; } public function __construct($id) { $this->SetId($id); } } // 子类继承父类 class Student extends Person { private $name; public function GetName() { return $this->name; } public function SetName($name) { $this->name = $name; } public function __construct($id,$name) { //调用父类的构造方法 Person::__construct($id); parent::__construct($id);//或者 $this->SetName($name); } } $stu = new Student(10001,"张三"); echo $stu->GetId().",".$stu->GetName();
输出:
10001,张三