Section IV - Constructor and Destructor - Classes and Objects in PHP5 [4]
Author: Leon Atkinson translation: Haohappy Source: Beyond the PHP / * ------------------------------------ ------------------------------------------ | = This article is HaohappY << CORE PHP Programming >> | = Classes and Objects Chapter Note | = Translate Main Personal Experience | = To avoid unnecessary troubles, please do not reprint, thank you | = Welcome criticism, hope and all PHP enthusiasts progress together! ----------------------------------------- ---------------------------------- * / Section 4 - Constructor and Destructor If you declare a function in a class, name it __construct, which will be used as a constructor and is executed when an object instance is created. Clearly, __ is two underscores. Just any other function Like the constructor, the constructor may have parameters or default. You can define a class to create an object and put it in one statement (statement). You can also define a function called __destruct, PHP will be in object Calling this function before being destroyed. It is called a destructor. Inherit is a powerful feature of the class. A class (subclass / derived class) can inherit the function of another class (parent / base class). Derive class will contain There are all properties and methods of the base class, and other properties and methods can be added to the derived class. You can also override the basic class method and properties. As shown in 3.1.2, you can use the extends keyword Inherited a class. 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 second chapter to mention :: Operator. 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 languages are named constructor after class. PHP The same is true for a few versions. That is: If you name animal, you are named Animal and create a naming is also animal method, this method is to construct a function. If a class is available while you have _ _Construt constructor and the same function as the class name, PHP will regard __construct as constructor. This allows the class written by the previous PHP version to still be used. But the new script (PHP5) should use the new declaration of the __construct. PHP to make the constructor have a unique name, no matter what the name of the class is. So you are changing the name of the class. When you don't need to change the name of the constructor. You may have a way of accessing the constructor in the PHP. Access mode will affect the ability to instantiate objects within a certain range. This allows for some fixed Design patterns, such as Singleton mode. Design function, conversely constructor. PHP calls them to destroy an object from memory. By default, PHP only releases the memory occupied by the object properties and destroys the object-related resources. Allows you to perform any code after using an object. When PHP determines that your script is no longer related to an object, the destructor will be called. In a function's namespace, this happens when the function return For global variables, this happens when the script is over. If you want to destroy an object clearly, you can assign any other value to the variable points to the object. Usually, the variable is assigned to NULL or call unset. The following example Calculate the number of objects from the objects from the class. The Counter class starts value from the constructor, impairment in the destructor. Once you define a class, you can use New to create an instance of this class. Class definition It is a design map, and the instance is a component placed on the assembly line. 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. Php class counter {Private Static $ count = 0; function __construct () {self: $ count ;