Home > Backend Development > PHP Tutorial > Basic code tutorial on php inheritance usage

Basic code tutorial on php inheritance usage

伊谢尔伦
Release: 2023-03-12 06:52:02
Original
978 people have browsed it

This article mainly introduces PHP object-orientedProgramming OOPInheritance usage, and analyzes the definition of php classes and inheritance usage methods in the form of simple examples. Friends in need can refer to Next

The examples in this article describe the usage of OOP inheritance in PHP 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 detailed content of Basic code tutorial on php inheritance usage. 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