深入探讨PHP类的继承
- < ?php
- class father{
- protected $name;
- function __construct($name){
- $this->name=$name;
- }
- function __destruct(){
- echo "< p>{$this->name}也是要死的< br/>< /p>";
- }
- //这个就是所谓的构造函数,用来初始化
- function go_to_sleeping(){
- echo "< p>{$this->name}想睡觉.< /p>";
- }
- function eat(){
- echo "< p>{$this->name}想吃饭.< /p>";
- }
- }
- class son extends father{
- function playing(){
- //小孩子会很调皮的,当然他也是要吃要睡的生物
- echo "< p>{$this->name}正在捣蛋...< /p>";
- }
- }
- $your_father=new father("老爸");
- $your_father->go_to_sleeping();
- $your_father->eat();
- $my_son=new son('宝贝');
- $my_son->go_to_sleeping();
- $my_son->eat();
- $my_son->playing();
- ?>
- < ?php
- class father{
- protected $name;
- function __construct($name){
- $this->name=$name;
- }
- function __destruct(){
- echo "< p>{$this->name}也是要死的< br/>< /p>";
- }
- //这个就是所谓的构造函数,用来初始化
- function go_to_sleeping(){
- echo "< p>{$this->name}想睡觉.< /p>";
- }
- function eat(){
- echo "<p>{$this->name}想吃饭.</p>";
- }
- }
- class son extends father{
- function playing(){
- //小孩子会很调皮的,当然他也是要吃要睡的生物
- echo "< p>{$this->name}正在捣蛋...< /p>";
- }
- }
- $your_father=new father("老爸");
- $your_father->go_to_sleeping();
- $your_father->eat();
- $my_son=new son('宝贝');
- $my_son->go_to_sleeping();
- $my_son->eat();
- $my_son->playing();
- ?>
标签: php