1. constructor: most classes have a special method called constructor. When an object is created, it automatically calls the constructor, that is, when the new keyword is used to instantiate the object, the constructor is automatically called. The constructor declaration is the same as the Declaration of other operations, but its name 1. constructor methods most classes have a special method called constructor. When an object is created, it automatically calls the constructor, that is, when the new keyword is used to instantiate the object, the constructor is automatically called. The constructor declaration is the same as that of other operations, but its name must be _ construct (). This is a change in PHP5. in previous versions, the name of the constructor must be the same as the class name, which can still be used in PHP5, but it is rarely used now, in this way, the constructor can be made independent of the class name. when the class name changes, you do not need to change the corresponding constructor name. For backward compatibility, if a class does not contain the _ construct () method, PHP searches for a write method in php4 and a constructor with the same name as the class name. Format: function _ construct ([parameter]) {...} only one constructor can be declared in a class. Instead, the constructor is called only once every time an object is created, so it is usually used to execute some useful initialization tasks. For example, assign an initial value to a property when creating an object. Example: [php] view plaincopy Initial value $ this-> name = $ name; // The $ sex passed in through the constructor gives the Member attributes $ this-> sex the initial value $ this-> sex = $ sex; // assign the $ age passed in by the constructor to the member attribute $ this-> age with the initial value $ this-> age = $ age ;} // function say () {echo "My name is :". $ this-> name. "Gender :". $ this-> sex. "My age is :". $ this-> age."
";}}// Create three objects $ p1, p2, and $ p3 through the constructor, name, gender, and age $ p1 = new Person ("3", "male", 22 ); $ p2 = new Person ("li 4", "female", 33); $ p3 = new Person ("Wang 5", "male", 44 ); // access the method of speaking in the $ p1 object below $ p1-> say (); // access the method of speaking in the $ p2 object below $ p2-> say (); // the following method is used to access the $ p3 object $ p3-> say ();?> Output result: my name is Zhang 3 Gender: Male, my age is 22, my name is Li 4 Gender: Female, my age is: 33 My name is Wang 5 Gender: Male my age is 44 2. destructor: The Destructor is opposite to the constructor. The Destructor is newly added to PHP5 and does not contain the Destructor in PHP4. The Destructor allows you to perform operations or complete some functions before destroying a class, such as closing a file and releasing a result set, the Destructor is executed when all references to an object are deleted or when the object is explicitly destroyed, that is, the destructor is called before the object is destroyed in the memory. Similar to the constructor name, the destructor name of a class must be _ destruct (). The Destructor cannot contain any parameters. Format: function _ destruct () {...} example:
Initial value $ this-> name = $ name; // The $ sex passed in through the constructor gives the Member attributes $ this-> sex the initial value $ this-> sex = $ sex; // assign the $ age passed in by the constructor to the member attribute $ this-> age with the initial value $ this-> age = $ age ;} // function say () {echo "My name is :". $ this-> name. "Gender :". $ this-> sex. "My age is :". $ this-> age."
";}// This is a destructor. before the object is destroyed, call function _ destruct () {echo" goodbye ". $ this-> name ."
";} // Create three objects $ p1, p2, and $ p3 through the constructor, name, gender, and age $ p1 = new Person ("3", "male", 22 ); $ p2 = new Person ("li 4", "female", 33); $ p3 = new Person ("Wang 5", "male", 44 ); // access the method of speaking in the $ p1 object below $ p1-> say (); // access the method of speaking in the $ p2 object below $ p2-> say (); // the following method is used to access the $ p3 object $ p3-> say ();?>
Output result: my name is Zhang 3 Gender: Male, my age is 22, my name is Li 4 Gender: Female, my age is: 33 My name is Wang 5 Gender: Male my age is: 44 destroy Zhang 3 destroy Li 4 destroy Wang 5