PHP Object-Oriented Programming OOP inheritance usage getting started example, object-oriented programming oop
This document describes the inheritance and usage of PHP Object-Oriented Programming (OOP. We will share this with you for your reference. The details are as follows:
<? Phpclass Person {var $ name; // protected var $ sex; var $ age; function _ construct ($ name = "", $ sex = "male ", $ age = 22) {$ this-> name = $ name; $ this-> sex = $ sex; $ this-> age = $ age;} function say () {echo $ this-> name. "Talking <br/>";} function run () {echo "walking... <br/>" ;}} class Student extends Person {var $ school; function _ construct ($ name = "", $ sex = "male", $ age = 22, $ school = "") {parent ::__ construct ($ n Ame, $ sex, $ age); $ this-> school = $ school;} function study () {echo $ this-> name. "in progress ". $ this-> school. "Learning <br/>" ;}} class Teacher extends Student {var $ wage; function teaching () {echo $ this-> name. "in progress ". $ this-> school. "teaching, the monthly salary is ". $ this-> wage. "<br/>" ;}}$ teacher1 = new Teacher ("kaifu", "male", 22); $ teacher1-> school = "edu "; $ teacher1-> wage = 4000; $ teacher1-> say (); $ teacher1-> study (); $ teacher1-> teaching ();?>
Result:
Kaifu is talking. kaifu is learning from edu. kaifu is teaching at edu. The monthly salary is 4000.