Structure and structure of PHP5.0 object model deep exploration

Source: Internet
Author: User
If you declare a function in a class and name it _ construct, this function will be treated as a structure function and executed when an object instance is created. To be clear, __is two underscores. Just like any other function, a schema function can be named _ construct if you declare a function in a class, this function will be treated as a schema function and executed when an object instance is created. To be clear, __is two underscores. Like any other function, a schema function 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 burned. it is called a destructor.

Persistence is a strong effect of the class. One class (subclass/derived class) can continue 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 continue a class.

You may want to know how structured functions are sustained. When they are sustained together with other methods, they are not executed when the object is created.

If you need this function, you need to use the operator mentioned in chapter 2. it promises to point to a namespace. parent points to the namespace of the parent class. you can use parent ::__ construct to call the structure function of the parent class.

Some object-oriented languages name structure functions 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 a structure function. if a class has both the _ construt structure function and a function with the same class name, PHP regards _ construct as the structure function. This allows the class written in the previous PHP version to still be applied. However, the new script (PHP5) should apply _ construct.

PHP's new method of declaring a schema function can make the schema function have a unique and unique name, no matter what the name of its class is. In this way, you do not need to change the name of the structure function when changing the class name.

You may give the structure function a visit method in PHP like other class methods. The visit method will affect the ability to instantiate objects from a certain scope. This promised to implement some fixed design patterns, such as the Singleton model.

Destructor. PHP calls them to burn an object out of memory. By default, PHP only releases the memory occupied by the object attribute and burns down the object-related resources. The Destructor promises you to execute any code after applying 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 will be generated when the function returns. for global variables, this occurs at the end of the script. if you want to burn 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, calculate the number of objects instantiated from the class. Counter class value-added from the beginning of the structure function, in the destructor impairment.

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 structure function requires a parameter, you should input the parameter after new.

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.