Copy codeThe Code is as follows:
<? Php
/*
* 1. Access to members of an object (in the internal method of an object, access other methods and member attributes of this object)
* 2. Each method in an object has a $ this keyword by default. this keyword indicates the object that calls this method.
*
* Constructor
*
* 1. It is the "first" and "automatically called" method after the object is created.
*
* 2. Definition of constructor. The method name is fixed,
* In php4: the same method as the class name is the constructor.
* In php5: the constructor selects to use the magic method _ construct (). This name is used to declare constructor in all classes.
* Advantage: the constructor does not need to be changed when the class name is changed.
* Magic method: Write a magic method in the class, and the corresponding function of this method will be added
* The method names are fixed (all provided by the system) and are not defined by the user.
* Every magic method is automatically called to complete a function at different times.
* Different magic methods have different call times.
* All Methods Starting _
* _ Construct (); _ destruct (); _ set ();......
*
* Function: Initialize the member attributes;
*
*
* Structure Method
*
* 1. method called automatically before the object is released
* The Garbage Collector (java php) is used, and c ++ is manually released.
*
* Function: close some resources for cleanup.
*
* _ Destruct ();
*
*/
Class Person {
Var $ name;
Var $ age;
Var $ sex;
// Php4 Constructor
/* Function Person ()
{
// Each declared object will call
Echo & quot; 1111111111111111 & quot ";
}*/
// Php5 Constructor
Function _ construct ($ name, $ age, $ sex ){
$ This-> name = $ name;
$ This-> age = $ age;
$ This-> sex = $ sex;
}
Function say (){
// $ This-> name; // use $ this for access to Members in the object
Echo "my name: {$ this-> name}, my age: {$ this-> age} <br>"
}
Function run (){
}
Function eat (){
}
// Destructor
Function _ destruct (){
}
}
$ P1 = new Person ("zhangsan", 25, "male ");
$ P2 = new Person;
$ P3 = new Person;