Section fourth constructors and destructors [4]_php Foundation

Source: Internet
Author: User

If you declare a function in a class named __construct, this function will be treated as a constructor and executed when an object instance is created. Clearly, __ is a two underline. Like any other function, the constructor may have parameters or default values. You can define a class to create an object and place its attributes all in one statement (statement).

You can also define a function called __destruct, and PHP will call this function before the object is destroyed. It is called a destructor.

Inheritance is a powerful feature of a class. A class (subclass/derived class) can inherit the functionality of another class (the parent class/base class). The derived class will contain all the properties and methods of the base class, and you can add other properties and methods to 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 inherit a 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.
If you need this feature, you need to use the following:: operator. It allows you to point to a namespace. Parent points to the parent class namespace, and you can use Parent::__construct to invoke the constructor of the parent class. Some object-oriented languages name constructors after a class. The same is true for the first few versions of PHP, which is still valid until now. That is, if you name a class animal and create a method in which the name is also animal, then this method is the constructor. If a class owns __ at the same time Construt constructors and functions that are the same as class names, PHP will treat __construct as a constructor. This allows classes written in previous versions of PHP to still be available. But the new script (PHP5) should use __construct. This new method of declaring a constructor in PHP allows the constructor to have a unique name, regardless of the name of the class in which it resides. So you don't need to change the name of the constructor when you change the name of the class. You might have given the constructor a way of accessing it in PHP like any other class method. The way of access will affect the ability to instantiate objects from within a certain range. This allows you to implement some fixed design patterns, such as singleton mode. destructors, rather than constructors. PHP calls them to destroy an object from memory. By default, PHP only frees the memory occupied by object properties and destroys object-related resources. Destructors allow you to clean up memory by executing arbitrary code after using an object. Destructors are invoked when PHP determines that your script is no longer related to objects. Within the namespace of a function, this occurs when the function return. 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 that points to that object. You usually assign a variable to duty as null or call unset. In the following example, the number of objects instantiated from the class is computed. The counter class starts to add value from the constructor, reducing the value in the destructor. Once you have defined a class, you can use new to create an instance of this class. A class is defined as 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 a parameter, you should enter the parameter after new. <?php
Class Counter
{
private static $count = 0; function __construct ()
{
Self:: $count + +;
} function __destruct ()
{
Self:: $count-;
} function GetCount ()
{
Return self:: $count;
}
//Establish the first instance
$c = new Counter (); Output 1
Print ($c->getcount (). "<br>n"); Create a second instance
$c 2 = new Counter (); Output 2
Print ($c->getcount (). "<br>n"); Destroying instances
$c 2 = NULL; Output 1
Print ($c->getcount (). "<br>n");
?> When you create a new instance, memory is prepared to store all the attributes. Each instance has its own set of properties that are unique. However, the method is shared by all instances of the class.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.