Construction methods and destructors are two special methods in objects that are related to the life cycle of an object. The construction method is
The first object is automatically called by the object after it is createdmethod, which is why we use the constructor method in the object. And the Destructor method is
object is automatically called by the object before it is destroyedmethod, which is why we use the Destructor method in the object. Therefore, it is common to use construction methods to accomplish some objects
Initialization Work, using the Destructor method to complete some objects before destroying
Cleanup Work。
1. Construction method
In each declared class there is a special member method called the constructor method, and if it is not explicitly declared, a constructor with no argument list and empty content is present in the class by default. If you explicitly declare it, the default constructor method in the class will not exist. When an object is created, the constructor is automatically called once, that is, the constructor is called automatically each time the object is instantiated with the keyword new, and the construction method cannot be invoked from the object's reference. Therefore, it is common to use construction methods to perform some useful initialization tasks, such as assigning an initial value to a member property when creating an object.
In previous versions of PHP5, the method name of the constructor must be the same as the class name, which could be used in PHP 5. However, the construction method with the same name in PHP is seldom declared, and the benefit is that the constructor is independent of the class name and does not need to change the corresponding constructor name when the class name changes. To be backwards compatible, when creating an object, if there is no construction method named construct () in a class, PHP will search for a construction method with the same name as the class name. The format for declaring a construction method in a class is as follows:
function construct ([parameter list]) {//constructor method name is a two-underscore //method body, typically used to initialize a member property to assign a value}
In PHP, only one construction method can be declared in the same class. The reason is that the constructor name is fixed and there is no way to declare two functions with the same name in PHP, so no method overloads are constructed. However, you can implement the ability to construct method overloads in other object-oriented programming languages by using default parameters when you declare a method to be constructed. This allows the member property to be initialized with a default parameter when the object is created, if no parameters are passed in the constructor method.
Constructors can accept parameters and be able to assign values to object properties when creating an object
Constructors can call class methods or other functions
Constructors can call constructors of other classes
Examples of constructor use:
<?phpclass person{ private $name; Private $age; Private $gender; Public function construct ($name, $age, $gender) { $this->setname ($name); Call class method $this->age = $age; $this->setgender ($gender); } Public Function SetName ($name) { $this->name = $name; } ... setter method} $person = new person ("Yeoman", 23, ' Male ');? >
Call the parent class constructor and call the constructor of the unrelated class:
function construct () { parent::construct ();//The constructor that invokes the parent class must display the parent class constructor that is called by using parent classname::construct (); Call constructors for other classes, ClassName is class name //Other operation}
Inheritance and Constructors
The constructor of a subclass in PHP does not actively invoke the constructor of the parent class, which is to be displayed using the Parent::construct () Call:
<?phpclass animal{ private $name; function construct ($name) { $this->setname ($name) echo "Animal class was created! "; } // ... Other methods}class Birds extends animal{ private $name; Private $leg; function construct ($name, $leg) { parent::construct ($name);//Display call $this->setleg ($leg); echo "Birds are created! "; } // ... Other methods}?>
If multiple inheritance is involved, when Parent::construct () is called, it is searched up the parent class until the most appropriate constructor is found, for example:
Then the example class Parrot extends birds{ private $name; Private $leg; Private $wing; function construct ($name) { parent::construct ($name); The appropriate constructor for the parent class (birds Class) is not found at this time, only the search is up and the animal class is searched for the appropriate constructors. echo "Parrot class was created! "; $this->smacktalk (); /* Output: "Animal class was created!" " the Parrot Speaks!" " * /} function Smacktalk () { echo" Parrot talking! "; }}
If you want to invoke the constructors of several parent classes in turn, you can call the constructors directly using the class name, for example:
function construct ($name, $leg) { animal::construct ($name);//Call Animal constructor birds::construct ($name, $leg);// Call Birds Constructor}
2. Destructors
The destructor allows certain actions to be performed before destroying an object, such as closing a file, releasing a result set, and so on.
When an object in a heap memory segment loses access to its reference, it cannot be accessed and becomes a garbage object. Usually the object's reference is given another value or the object loses its reference at the end of the page run.
Destructors are called automatically when objects are destroyed, and cannot be called explicitly. Destructors cannot take parameters.
The declaration format for the destructor method is as follows:
function deconstruct () { //method body, usually used to complete some cleanup tasks before object destruction}
Destructors may be called (but not necessarily) in the following situations:
After the PHP page has finished loading;
Unset () class;
When a variable reference points to another object or value;
PHP's memory recovery mechanism is similar to that of Java, where no reference objects are destroyed and recycled, using a reference counter technique.
Example:
<?phpclass test{ Function destruct () { echo] is called when the object is destroyed!!! "; }} $a = $b = $c = new test (); $a = Null;unset ($b); echo "
In this case, for example, there are three variables that reference $ A, $b, $c point to the test object, the test object has 3 reference counts, and when $ A = NULL, $a the reference to the test object is lost, the count-1, becomes 2, and when $b is unset (), $b reference to the test object is lost, The count is 1, becomes 1, the last page is loaded, the reference to the test object is automatically freed, the count is then 1, and the 0,test object is destroyed without a variable reference, and the destructor is called.
The destructor method in PHP is not very common, it is an optional part of the class and is declared in the class only if needed.
<?phpclass person{ var $name; var $sex; var $age; function construct ($name, $sex, $age) { $this->name = $name; $this->sex = $sex; $this->age = $age; } function Destruct () { echo "Goodbye". $this->name. "<br/>"; }} $person 1 = new Person ("33", "male", and at all); $person 1 = null; The first object will lose the reference $person2 = new Person ("Li 44", "female", "N"), $person 3 = new Person ("Wang 55", "male", 43); >
Operation Result:
Goodbye Zhang 33 Goodbye Wang 55 goodbye Lee 44
The first object after the declaration is completed, its reference is given a null value, so the first object loses the reference, can no longer be accessed, after the person automatically call the first object of the destructor output "Goodbye 33". The two objects that are declared later are references lost at the end of the page execution, and the destructor is automatically called. However, because the object's reference is placed in the stack memory , because the stack's LIFO feature, the last created object will be released first, many to automatically call the third object's destructor, and finally call the second object's destructor method.