PHP5 object-oriented programming_PHP tutorial

WBOY
Release: 2016-07-21 15:53:31
Original
913 people have browsed it

PHP5 has a single-inherited, restricted-access, overloadable object model. "Inheritance," discussed in detail later in this chapter, involves parent-child relationships between classes. In addition, PHP supports restrictions on properties and methods. Access. You can declare members as private, disallowing access from outside classes. Finally, PHP allows a subclass to overload members from its parent class.

PHP5’s object model treats objects as any other data Different types, passed by reference. PHP does not require you to explicitly pass and return objects by reference. The reference-based object model will be explained in detail at the end of this chapter. It is the most important new feature in PHP5.

With a more direct object model, there are additional advantages: increased efficiency, less memory usage, and greater flexibility.

In previous versions of PHP, scripts The object is copied by default. Now PHP5 only moves the handle, which takes less time. The improvement in script execution efficiency is due to the avoidance of unnecessary copying. While the object system brings complexity, it also brings benefits in execution efficiency. At the same time, reducing copying means taking up less memory, leaving more memory for other operations, which also improves efficiency.

Zand Engine 2 has greater flexibility. A happy development It allows destruction - executing a class method before the object is destroyed. This is also good for utilizing memory, allowing PHP to clearly know when there are no references to the object and allocate the vacated memory to other uses.

Supplement:

Memory management of PHP5

Object transfer



PHP5 uses Zend Engine II, and objects are stored in an independent structure Object Store , instead of being stored in Zval like other general variables (in PHP4, objects are stored in Zval like general variables). Only the pointer of the object is stored in Zval rather than the content (value). When we copy an object or pass an object as a parameter to a function, we do not need to copy the data. Just keep the same object pointer and let another zval notify the Object Store that this particular object now points to. Since the object itself is located in the Object Store, any changes we make to it will affect all zval structures holding pointers to the object - manifested in the program as any changes to the target object will affect the source object. .This makes PHP objects look like they are always passed by reference, so objects in PHP are passed by "reference" by default and you no longer need to use & to declare them like in PHP4.



Garbage collection mechanism

Some languages, most typically C, require you to explicitly request memory allocation when you create a data structure. Once you allocate memory, you can store information in variables. At the same time, you also need to release the memory when you are finished using the variable, which allows the machine to free up memory for other variables and avoid running out of memory.

PHP can automatically manage memory and clear objects that are no longer needed. PHP uses reference counting, a simple garbage collection mechanism. Each object contains a reference counter, and each reference connected to the object increases the counter by one. When reference leaves the living space or is set to NULL, the counter is decremented by 1. When an object's reference counter reaches zero, PHP knows that you no longer need to use this object and releases the memory space it occupies.

For example:

Copy code The code is as follows:

class Person{
}
function sendEmailTo(){
}

$haohappy = new Person();
// Create a new object: Reference count Reference count = 1
$haohappy2 = $haohappy;
//Copy by reference: Reference count = 2
unset($haohappy); 2 ; 🎜>
unset($haohappy2);    
//Delete reference: Reference count = 0 automatically release memory space

?>


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/318700.htmlTechArticlePHP5 has a single inheritance, restricted access, and overloadable object model. More details later in this chapter The "inheritance" discussed includes the parent-child relationship between classes. In addition, PHP supports restrictions on properties and methods...
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template