- // 建立基类 Animal
- class Animal
- {
- public $name; // 基类的属性, 名字 $name
- // 基类的构造函数, 初始化赋值
- public function __construct($name)
- {
- $this->name = $name;
- }
- }
- // 定义派生类 Person 继承自 Animal 类
- class Person extends Animal
- {
- public $personSex; // 对于派生类, 新定义了属性 $personSex 性别 $personAge 年龄
- public $personAge;
- // 派生类的构造函数
- function __construct( $personSex, $personAge )
- {
- parent::__construct( "PBPHome" ); // 使用 parent 调用了父类的构造函数 语句
- $this->personSex = $personSex;
- $this->personAge = $personAge;
- }
- // 派生类的成员函数, 用于打印, 格式: 名字 is name,age is 年龄
- function printPerson()
- {
- print( $this->name. "is" .$this->personSex. ",age is" .$this->personAge );
- }
- }
- // 实例化 Person 对象
- $personObject = new Person( "male", "21");
- // 执行打印
- $personObject->printPerson(); // 输出结果: PBPHome is male,age is 21
- ?>
来源: https://www.php1.cn/detail/php-add74db6bf.html