Section IV - Constructor and Destructor If you declare a function in a class, name __construct, this function will be used as a constructor and is executed when an object instance is created. Clearly, _ _ Is two underscores. Just like any other function, the constructor may have parameters or default. You can define a class to create an object and put its properties in a statement (statement). You can also define one The function named __destruct, PHP will call this function before being designed. It is called a destructor. Inherit is a powerful function of the class. A class (subclass / derived class) can inherit another class (parent class) / Base class) The function. Debuting classes will contain all properties and methods of base classes, and can add other properties and methods in derived classes. You can also override the method and properties of the base class. Like 3.1.2 Displayed, you can inherit a class with an Extends key. You may want to know how the constructor is inherited. When they are inherited with other methods, they will not be executed when they create an object. If you need This feature, you need to use the :: operator with the second chapter. It allows you to point to a namespace. Parent points to the parent class namespace, you can use Parent :: __ construct to call the parent class constructor. Some object-oriented objects The language is named after the class. The first few versions of PHP are still effective. That is: If you name animal, it is also animal method, this method is also animal. It is a constructor. If a class has a function of the __constut constructor and the same function as the class name, PHP will treat __construct as constructor. This makes the class written by the previous PHP version still use. But new The script (PHP5) should use the new declaration constructor of __construct. PHP to make the constructor have a unique name, regardless of the name of the class it, so that you are changing the name of the class, There is no need to change the name of the constructor. You may give constructor in the PHP to access the same way as other class methods. Accessing methods will affect the ability of objects from a certain range. This allows some fixed design patterns, Such as a Singleton mode. Destructor, the Configure function. PHP calls them to destroy an object from memory. By default, PHP only releases the memory occupied by the object property, and destroys the object-related resources. Destructor Allow you Use an object to perform any code to clear the memory. When PHP determines that your script is no longer related to an object, the destructor will be called. In the namespace of a function, this happens when the function return is. At the global variable, this happened at the end of the script. If you want to destroy an object clearly, you can assign any other value to the variable to this object. The variable is usually assigned to NULL or the unset. In the example below, Calculate the number of objects of objects that are instantiated from the class. The Counter class starts value from the constructor, impairment in the destructive function. Once you define a class, you can use New to create an instance of this class. Class definition is The design, the instance is a component placed on the assembler. New requires the name of the class and returns an instance of the class. If the constructor requires parameters, you should enter the parameters after New.
Class counter {private static $ count = 0; function __construct () {self: $ count ;} function __destruct () {Self: $ count -;} function getcount () {Return Self: $ count;}} // Establish the first example $ c = new counter (); // Output 1 print ($ c-> getcount (). "N"); // Establish a second instance $ c2 = new counter (); / / Output 2 Print ($ C-> getCount (). "N"); // Destroy instance $ c2 = null; // Output 1 print ($ c-> getcount (). "N");?>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
When you create a new instance, memory will be prepared to store all properties. Each instance has its own unique set of properties. But the method is shared by all instances of this class.