: This article mainly introduces an example of understanding PHP object-oriented programming (OOP). If you are interested in PHP tutorials, please refer to it.
Class Person {
// Declare a new public variable $ name, which can be accessed by classes in any package
Public $ name; public $ sex; public $ age;
// Declare a method of this class function say () {echo "Hello!
"; Echo" My name is: ". $ this-> name ."
"; Echo" my gender is: ". $ this-> sex ."
";}}
// 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 ."
I am going to school at ". $ 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.
The above introduces an example of understanding PHP object-oriented programming (OOP), including some content, and hopes to help those who are interested in PHP tutorials.