Home > php教程 > PHP开发 > Constructor and destructor

Constructor and destructor

黄舟
Release: 2016-12-14 11:10:12
Original
1251 people have browsed it

If you declare a function in a class and name it __construct, this function will be treated as a constructor and executed when creating an object instance. To be clear, __ is two underscores. Just like any other Like functions, constructors may have parameters or default values. You can define a class to create an object and put all its properties in a statement.

 You can also define a function named __destruct, PHP This function will be called before the object is destroyed. It is called a destructor.

Continuation is a powerful feature of classes. One class (subclass/derived class) can continue the functionality of another class (parent class/base class) . The derived class will contain all the properties and methods of the base class, and can add other properties and methods in the derived class. You can also override the methods and properties of the base class. As shown in 3.1.2, you can Use the extends keyword to extend a class.
You may be wondering how constructors are extended. When they are extended along with other methods, they are not executed when the object is created.
If you need this functionality, you You need to use the :: operator mentioned in Chapter 2. It allows you to point to a namespace. parent points to the parent class namespace. You can use parent::__construct to call the constructor of the parent class.

Some object-oriented languages ​​​​Name the constructor after the class. This was also true in previous versions of PHP, and 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 Function. If a class has both a __construt constructor and a function with the same name as the class, PHP will treat __construct as a constructor. This allows classes written in previous PHP versions to still be used. But new scripts ( PHP5) You should use __construct.

This new method of declaring a constructor in PHP allows the constructor to have a unique name, no matter what the name of the class it is in is. In this way, when you change the name of the class, There is no need to change the name of the constructor.

You may give the constructor in PHP an access method like other class methods. The access method will affect the ability to instantiate objects from a certain scope. This allows to implement some fixed designs Patterns, such as the Singleton pattern.

Destructors are the opposite of constructors. PHP calls them to destroy an object from memory. By default, PHP only releases the memory occupied by the object's properties and destroys the resources related to the object. Destruction Functions allow you to clear memory by executing arbitrary code after using an object.

When PHP decides that your script is no longer related to the object, the destructor will be called. Within a function's namespace, this will happen within the function's namespace. When returning. 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 pointing to the object. Usually assign the variable to NULL or call unset.

 The following example counts the number of objects instantiated from the class. The Counter class is incremented starting from the constructor and decremented in the destructor.

Once you have defined a class, you can use new to create this Instance of the class. The definition of the class is the design drawing, and the instance is the 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.


class Counter
{
private static $count = 0;

function __construct()
{
self::$count ;
}

function __destruct()
{
self::$count --;
}

function getCount()
{
return self::$count;
}
}

//Create the first instance
$c = new Counter();

//Output 1
print($c->getCount() . "
n");

//Create a second instance
$c2 = new Counter();

//Output 2
print($c- >getCount() . "
n");

//Destroy the 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 methods are shared by all instances of the class.

For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template