PHP面向对象程序设计OOP继承用法详解

Posted on Posted in 时事八卦, 最新资讯

这篇文章主要介绍了PHP面向对象程序设计OOP继承用法,结合简单实例形式分析了php类的定义与继承使用方法,需要的朋友可以参考下

本文实例讲述了PHP面向对象程序设计OOP继承用法。分享给大家供大家参考,具体如下:

<?php
class Person {
  var $name;//protected
  var $sex;
  var $age;
  function __construct($name = "", $sex = "男", $age = 22) {
    $this->name = $name;
    $this->sex = $sex;
    $this->age = $age;
  }
  function say() {
    echo $this->name . "在说话<br/>";
  }
  function run() {
    echo "在走路·<br/>";
  }
}
class Student extends Person {
  var $school;
  function __construct($name = "", $sex = "男", $age = 22,$school="") {
    parent::__construct($name,$sex,$age);
    $this->school = $school;
  }
  function study() {
    echo $this->name."正在".$this->school."学习<br/>";
  }
}
class Teacher extends Student {
  var $wage;
  function teaching() {
    echo $this->name."正在".$this->school."教学,每月工资为".$this->wage."<br/>";
  }
}
$teacher1 = new Teacher("kaifu","男",22);
$teacher1->school = "edu";
$teacher1->wage = 4000;
$teacher1->say();
$teacher1->study();
$teacher1->teaching();
?>

结果:

kaifu在说话
kaifu正在edu学习
kaifu正在edu教学,每月工资为4000

以上就是本文的全部内容,希望对大家的学习有所帮助。

相关推荐:

PHP采集类snoopy实例介绍

PHP OOP之后期静态绑定

php使用snoopy与curl模拟登陆的实例分享

以上就是PHP面向对象程序设计OOP继承用法详解的详细内容,更多请关注php中文网其它相关文章!

该文章由WP-AutoPost插件自动采集发布

原文地址:http://www.php.cn/php-weizijiaocheng-398890.html

发表评论

电子邮件地址不会被公开。 必填项已用*标注