Detailed explanation of OOP inheritance usage in PHP object-oriented programming

墨辰丷
Release: 2023-03-28 10:06:01
Original
1422 people have browsed it

This article mainly introduces the usage of OOP inheritance in PHP object-oriented programming, and analyzes the definition and inheritance usage of PHP classes in the form of simple examples. Friends in need can refer to it

The examples in this article describe PHP OOP inheritance usage in object-oriented programming. Share it with everyone for your reference, the details are as follows:

<?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();
?>
Copy after login

Result:

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

The above is the entire content of this article, I hope it will be helpful to everyone's study.


Related recommendations:

PHP collection class snoopy example introduction

Post static binding after PHP OOP

php uses snoopy to share examples of simulated login with curl

The above is the detailed content of Detailed explanation of OOP inheritance usage in PHP object-oriented programming. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template