Everyone is rightIf you declare a function in a class and name it _ construct, this function will be treated as a constructor and executed when an object instance is created. Clearly, __is two underscores. Like any other function, PHP 5.0 constructor may have parameters or default values. You can define a class to create an object and put all its attributes in a statement.
You can also define a function named _ destruct. PHP will call this function before the object is destroyed. It is called a destructor.
Inheritance is a powerful function of a class. A class (subclass/derived class) can inherit the functions of another class (parent class/base class. the derived class contains all the attributes and methods of the base class, and you can add other attributes and methods to the derived class. You can also override the methods and attributes of the base class. As shown in the previous article, you can use the extends keyword to inherit a class.
You may want to know How PHP 5.0 constructor is inherited. When they are inherited together with other methods, they are not executed when the object is created.
If you need this function, you need to use: operator. it allows you to point to a namespace. parent points to the namespace of the parent class. You can use parent ::__ construct to call the constructor of the parent class.
Some object-oriented languages name constructors after the class. The same is true for the first few versions of PHP. This method is still valid. that is: if you name a class Animal and create a method named Animal in it, this method is the constructor. if a class has both the _ construt constructor and the same class name, PHP regards _ construct as the constructor. This makes the class written in the previous PHP version still usable. But the new script (PHP5) should use _ construct.
PHP's new method of declaring constructor allows PHP 5.0 constructor to have a unique name, regardless of the name of the class it belongs. In this way, you do not need to change the name of the constructor when changing the class name.
You may give the constructor the same access method as other class methods in PHP. The access method will affect the ability to instantiate objects within a certain range. This allows some fixed design patterns, such as the Singleton mode.
Destructor, opposite to constructor. PHP calls them to destroy an object from the memory. By default, PHP only releases the memory occupied by object properties and destroys object-related resources. The Destructor allows you to execute arbitrary code to clear the memory after using an object.
When PHP decides that your script is no longer related to objects, the Destructor will be called. in the namespace of a function, this occurs when the function returns. for global variables, this occurs at the end of the script. if you want to explicitly destroy an object, you can assign any other value to the variable pointing to the object. usually, the variable value is NULL or unset is called.
In the following example, the number of objects instantiated from the class is calculated. The Counter class starts to increase value from the PHP 5.0 constructor, And the Destructor is impaired.
- Class Counter
- {
- Private static $Count=0;
-
- Function _ construct ()
- {
- Self: $ count ++;
- }
-
- Function _ destruct ()
- {
- Self: $ count --;
- }
-
- Function getCount ()
- {
- Return self: $ count;
- }
- }
-
- // Create the first instance
- $C=NewCounter ();
-
- // Output 1
- Print ($ c-> getCount (). "n ");
-
- // Create the second instance
- $C2=NewCounter ();
-
- // Output 2
- Print ($ c-> getCount (). "n ");
-
- // Destroy the instance
- $C2=NULL;
-
- // Output 1
- Print ($ c-> getCount (). "n ");
- ?>
Once you define a class, you can use new to create an instance of this class. A class is defined as a design drawing, and an instance is a component placed on an assembly line. new Requires the class name and returns an instance of the class. If the PHP 5.0 constructor requires a parameter, you should enter the parameter after new.