Section 4 -- constructor and destructor -- ClassesandObjectsinPHP5 [4] _ PHP Tutorial

Source: Internet
Author: User
Section 4-constructor and Destructor-ClassesandObjectsinPHP5 [4]. Section 4 -- constructor and Destructor if you declare a function in a class, name it _ construct, this function will be treated as a constructor and will be Section 4: constructor and Destructor

If 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, 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 other attributes and methods can be added to the derived class. you can also override the methods and attributes of the base class. as shown in 3.1.2, you can use the extends keyword to inherit a class.

You may want to know how constructors are inherited. when they are inherited together with other methods, they are not executed during object creation.
If you need this function, you need to use the operator mentioned in chapter 2. 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 constructor 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 can make the constructor have a unique name, no matter what the name of its class is. 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 after using an object to clear the memory.

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 is increased from the constructor and impaired in the destructor.

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 constructor requires a parameter, you should enter the parameter after new.

 GetCount ()."
"); // Create the second instance $ c2 = new Counter (); // output 2 print ($ c-> getCount ()."
"); // Destroy instance $ c2 = NULL; // output 1 print ($ c-> getCount ()."
");?>





When you create an instance, the memory will be prepared to store all attributes. each instance has its own unique set of attributes, but the method is shared by all instances of this class.

If you declare a function in a class and name it _ construct, this function will be treated as a constructor and will be called when an object instance is created...

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.