1. Construction methods Most classes have a special method called constructors. When an object is created, it automatically invokes the constructor, which means that the constructor is invoked automatically when the object is instantiated using the New keyword. The declaration of the constructor is the same as the declaration of other operations, except that its name must be __construct (). This is the change in the PHP5, in the previous version, the name of the constructor must be the same as the class name, which is still available in PHP5, but is now rarely used, the advantage of which is that the constructor function is independent of the class name, and the corresponding constructor name does not need to be changed when the class name changes. For backward compatibility, if a class does not have a method named __construct (), PHP will search for a PhP4 write method that has the same name as the class name. Format: function __construct ([parameters]) {...}} can declare only one construction method in a class, but only once a construction method is invoked every time an object is created, and it is not active to invoke this method, so it is usually used to perform some useful initialization tasks. For example, the attribute is assigned an initial value when the object is created. Example: [PHP] View plaincopy //Create a human class person { //below is a member of the human attributes var $name; var $s Ex var $age; //define a constructor parameter for $name, $sex and $age function __construct ($name, $sex, $age) {&N Bsp The $name to the member attribute $this->name the initial value $this->name= $name by constructing the method. //through the construction method $sex to the member attribute $this->sex the initial value $this->sex= $sex; //$age to member properties through the construction method $thisThe first->age value $this->age= $age; } //This person's way of speaking function say () { echo "My name is called:". $this->name. "Sex:" $this->sex. "My Age is : ". $this->age." <br> "; } } //create 3 objects by constructing methods $p1, P2, $p 3, passed into three different arguments for name, gender and age $p 1=new person ("Zhang 3", "male", 22); $p 2=new person ("Li 4", "female", 33); $p 3=new person ("Wang 5", "Male", 44); //below access to the $p1 object in the speech method $p 1->say (); //below access to the $p2 object in the speech method $p 2->say (); //below access to the $p3 object in the speech method $p 3->say (); ?> Output results are: My name is called: Zhang 3 Sex: Male my age is: 22 My name is called: Lee 4 Sex: Female my age is: 33 My name son: Wang 5 Sex: Male My age is: 2. destructor: &N Bsp The is relative to the constructor function. The destructor is a newly added content of PHP5, and there is no destructor in PHP4. Destructors allow you to perform some operations or perform some functions before destroying a class, for example, to close a file, release a result set, and so on, the destructor is executed when all references to an object are deleted or when the object is explicitly destroyed, that is, the object is called before it is destroyed in memory. Like the name of the constructor, the destructor name of a class must be __destruct (). Destructors cannot have any arguments. Format: function __destruct () {...} Example:
<span style= "color: #333333;"
><?
Create a human class person {//below is the member attribute of the human var $name;
var $sex;
var $age;
Defines a constructor parameter for the $name, $sex, and $age function __construct ($name, $sex, $age) {//$name to the member property $this->name the first value
$this->name= $name;
The $sex to the member attribute $this->sex the initial value $this->sex= $sex by constructing the method.
The $age to the member attribute $this->age the initial value $this->age= $age by constructing the method. //This person's speaking method function say () {echo "My name is called:". $this->name. "Sex:". $this->sex. "My Age is:" $this->age. "
<br> "; //This is a destructor that invokes function __destruct () {echo "Goodbye" before the object is destroyed. $this->name. "
<br> ";
///Create 3 objects by construction method $p1, P2, $p 3, passing in three different arguments for name, gender and age $p 1=new person ("Zhang 3", "male", 22);
$p 2=new person ("Li 4", "female", 33);
$p 3=new Person ("Wang 5", "Male", 44);
The following accesses the speech method in the $p1 object $p 1->say ();
The following accesses the speech method in the $p2 object $p 2->say ();
The following accesses the speech method in the $p3 object $p 3->say (); ?></span>
The output is: My name is called: Zhang 3 Sex: Male my age is: 22 My name son is called: Lee 4 Sex: Female my age is: 33 My name son: Wang 5 Sex: Male My age is: 44 destroys Zhang 3 destroys Li 4 destroys the King 5