PHP5 object-oriented programming

Source: Internet
Author: User
PHP5 object-oriented programming PHP5 has a single-inherited, restricted-access, and reloaded object model. this chapter will discuss inheritance in detail later, including the parent-child relationship between classes. in addition, PHP supports restricted access to attributes and methods. you can declare that the members are private and do not allow external Class access. finally, PHP allows a subclass to reload members from its parent class.

The object model of PHP5 treats an object as different from any other data type and is passed through reference. PHP does not require you to pass and return objects through reference explicitly. at the end of this chapter, we will elaborate on the reference-based object model. it is the most important new feature in PHP5.

With a more direct object model, it has additional advantages: improved efficiency, less memory usage, and greater flexibility.

In the first few versions of PHP, the script copies objects by default. now, PHP5 only moves the handle, which takes less time. the script execution efficiency is improved because unnecessary copying is avoided. while the object system brings complexity, it also brings the benefit of execution efficiency. at the same time, reducing replication means that less memory is occupied and more memory can be set aside for other operations, which also improves the efficiency.

Zand engine 2 has greater flexibility. a happy development is to allow the destructor to execute a class method before the object is destroyed. this is also very good for the use of memory, so that PHP can clearly know when there is no object reference, and allocate the empty memory to other purposes.

Supplement:

PHP5 memory management

Object transfer



PHP5 uses Zend Engine II, and objects are stored in an independent structure Object Store, unlike other common variables stored in Zval (objects in PHP4 are stored in Zval like General variables ). In Zval, only the object pointer is stored, not the content (value ). When we copy an object or pass an object as a parameter to a function, we do not need to copy data. Only keep the same Object pointer and the other zval notifies the Object Store to which the specified Object points. Because the Object is located in the Object Store, any changes we make to it will affect all the zval structures that hold the object pointer-any changes to the target object in the program will affect the source object .. This makes PHP Objects look like they are always passed through reference, so objects in PHP are passed through reference by default, you no longer need to use & As in PHP4.



Garbage collection mechanism

Some languages, such as C, require you to explicitly allocate memory when you create a data structure. Once allocated to the memory, you can store information in the variable. At the same time, you also need to release the memory when you stop using the variable, so that the machine can leave the memory to other variables to avoid consumption of memory.

PHP can automatically perform memory management to clear unnecessary objects. PHP uses reference counting, a simple garbage collection mechanism. Each object contains a reference counter. each reference is connected to an object, and the counter is incremented by 1. When the reference leaves the living space or is set to NULL, the counter minus 1. When the reference counter of an object is zero, PHP knows that you no longer need to use this object, releasing the memory space occupied by it.

For example:

The code is as follows:


Class Person {
}
Function sendEmailTo (){
}

$ Haohappy = new Person ();
// Create a new object: Reference count = 1
$ Haohappy2 = $ haohappy;
// Copy by Reference: Reference count = 2
Unset ($ haohappy );
// Delete a Reference: Reference count = 1
SendEmailTo ($ haohappy2 );
// Pass the object through reference:
// During function execution:
// Reference count = 2
// After execution:
// Reference count = 1

Unset ($ haohappy2 );
// Delete Reference: Reference count = 0 automatically releases memory space

?>

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.