An example of understanding PHP Object-Oriented Programming (OOP), Object-Oriented Programming (oop)
<? Php
Class Person {
// Declare a new public variable$ NameCan be accessed by classes in any package
Public $ name; public $ sex; public $ age;
// Declare a method of this class function say () {echo "Hello! <Br/> "; echo" My name is :". $ this-> name. "<br/>"; echo "My gender is :". $ this-> sex. "<br/> ";}}
// Declare the new Student class as the Child class of Person Student extends Person {public $ school; function study (){
// Parent: can be used to call the member methods defined in the parent class.
Parent: say (); echo "My age is:". $ this-> age. "<br/> I am going to school.". $ this-> school. ";}}
// You only need to define P1 as a Student class $ p1 = new Student ();
// Assign $ P1-> name = "zhangsan"; $ p1-> sex = "male" to the name, sex, age, and school attributes of p1 "; $ p1-> age = "18"; $ p1-> school = "XX school"; $ p1-> study ();?>
The browser output result is as follows:
Hello!
My name is: Zhang San
My gender is: Male
My age is: 18
I am going to school at XX School.