Constructors are the basic syntax in PHP, and what are constructors? Let's take a look at the PHP constructor here. If you declare a function in a class named __construct, the function will be treated as a constructor and executed when an object instance is created. Clearly, __ is a two underline. Like any other function, Constructors may have parameters or default values. You can define a class to create an object and put its properties all in a single statement (statement).
You can also define a function called __destruct, which PHP will call before the object is destroyed. It is called a destructor. Inheritance is a powerful feature of classes. A class (subclass/derived class) can inherit the functionality of another class (parent class/base class). A derived class will contain all the properties and methods of the base class. You can add additional properties and methods to the derived class. You can also override the methods and properties of the base class. You might want to know how constructors are inherited. When they are inherited with other methods, they are not executed when the object is created.
Some object-oriented languages name constructors after the class. This is true for the first few versions of PHP, which is still valid. That is, if you name a class animal and you create a name in it that is also animal, this method is the constructor. If a class has __ Construt constructor and the same function as the class name, PHP will treat __construct as a constructor. This makes the classes written in the previous PHP version still available. But the new script (PHP5) should use __construct. This new method of declaring constructors in PHP allows constructors to have a unique name, regardless of the name of the class in which it resides. So when you change the name of a class, you don't need to change the name of the constructor.
PHP constructors are accessed in the same way as other class methods. Access will affect the ability to instantiate objects from a range. This allows for some fixed design patterns, such as singleton mode. Destructors, as opposed to constructors. PHP calls them to destroy an object from memory. By default, PHP simply frees the memory occupied by the object's properties and destroys the object-related resources. Destructors allow you to execute arbitrary code after using an object to clear memory. When PHP decides that your script is no longer relevant to the object, The destructor is called. Within the namespace of a function, this occurs when the function return. For global variables, this happens at the end of the script. If you want to explicitly destroy an object, You can assign any other value to the variable that points to the object. The variable is usually assigned on duty to null or called unset.
In the following example, the number of objects instantiated from a class is computed. The counter class starts from the constructor and adds value to the destructor. Once you have defined a class, you can use new to create an instance of the class. The definition of a class is a design diagram, and an instance is a component placed on an 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
- Classcounter
- {
- privatestatic$ Count = 0 ;
- Function__construct ()
- {
- Self:: $count + +;
- }
- Function__destruct ()
- {
- Self:: $count--;
- }
- Functiongetcount ()
- {
- Returnself:: $count;
- }
- }
- Establish the first instance
- $ C = Newcounter ();
- Output 1
- Print ($c->getcount (). " <br> n ");
- Create a second instance
- $ C2 = Newcounter ();
- Output 2
- Print ($c->getcount (). " <br> n ");
- Destroying instances
- $ C2 = NULL ;
- Output 1
- Print ($c->getcount (). " <br> n ");
- ?>
When you create a new instance, the memory is prepared to store all the properties. Each instance has its own unique set of properties. However, the method is shared by all instances of the class.
http://www.bkjia.com/PHPjc/446477.html www.bkjia.com true http://www.bkjia.com/PHPjc/446477.html techarticle constructors are the basic syntax in PHP, and what are constructors? Let's take a look at the PHP constructor here. If you declare a function in a class, name it __constr ...