//例子 动物类与犬类的关系。 //animal.php class Animal{ private $weight; public function getWeight(){ return $this->weight; } public function setWeight($w){ $this->weight = $w; } } ?>
<? //Dog类继承自animal 类。 //dog.php require_once('animal.php'); class Dog extends Animal{ /** *子类新增方法 */ public function Bark(){ echo "Wang ~!~! Wang ~!~!"; } } ?>
echo "myDog`s weight is ".$myDog->getWeight()."<br>"; $myDog->Bark(); ?>
<? /** * 有些资料上说PHP5的构造函数不被继承。 * 演示的结果证明,PHP5的构造函数被继承了。 * 当子类Dog1被实例化时,继承的构造函数被调用了。 * 屏幕上显示了一句 "I am an Animal.". */ class Animal{ public $legNum = 0; public function __construct(){ $this->legNum = 4; echo "I am on animal <br>"; } }