PHP has a single-inherited, restricted-access, object model that can be overloaded. In addition, PHP supports restrictive access to properties and methods. You can declare members to be private and do not allow external classes to be accessed. Finally, PHP allows a subclass to overload members from its parent class.
The object model of PHP regards objects as different from any other data type and is passed by reference. PHP does not require you to pass and return objects explicitly by reference (reference). The reference-based object model is described in detail at the end of this chapter. It is the most important new feature in PHP5.
With a more straightforward object model, there are additional advantages: increased efficiency, less memory, and greater flexibility.
In the first few versions of PHP, scripts replicate objects by default. PHP now moves only the handle, which takes less time. The increase in script execution efficiency is due to the avoidance of unnecessary replication. While the object system brings complexity, it also brings the benefit of execution efficiency. At the same time, reducing replication means taking up less memory and allowing more memory for other operations, which also increases efficiency.
The Zand Engine 2 offers greater flexibility. A pleasing development is the permission to refactor-a class method is executed before the object is destroyed. This is also good for using memory, allowing PHP to clearly know when there is no reference to the object and allocating the vacated memory to other uses.
Add:
PHP5 Memory Management
Object passing
PHP5 uses Zend Engine II, objects are stored in a separate structure object store, not stored in zval like other general variables (stored in Zval as objects in PHP4 and general variables). Only pointers to objects, not content (value), are stored in zval. 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 be notified by another zval now that this particular object is pointing to the object Store. Since the object itself is in object Store, any changes we make to it will affect all zval structures that hold the pointer to the object----in the program that any change to the target object will affect the source object: This makes the PHP object look like always by reference (reference ), so the object in PHP is passed by "reference" by default, and you no longer need to declare it using & As in PHP4.
Garbage collection mechanism
Some languages, most typically such as C, require you to explicitly request the allocation of memory when you create a data structure. Once you have allocated memory, you can store the information in the variable. You also need to release the memory at the end of the use of the variable, which allows the machine to empty out memory to other variables and avoid consuming light memory.
PHP can automate memory management to remove objects that are no longer needed. PHP uses the simple garbage collection (garbage collection) mechanism of the reference count (reference counting). Each object contains a reference counter, each reference connected to the object, and the counter is incremented by 1. When reference leaves the living space or is set to NULL, the counter is reduced by 1. When a reference counter for an object is zero, PHP knows that you will no longer need to use this object to free up the memory space it occupies.
For example:
The code is as follows:
<?php class person{} function Sendemailto () {} $haohappy = new Person ( ); Create a new object: reference count Reference count = 1 $haohappy 2 = $haohappy; Copy by reference: Reference count = 2 unset ($haohappy); Delete a reference: Reference count = 1 Sendemailto ($haohappy 2); Passing objects by reference://During function execution://Reference count = 2//After execution://Reference count = 1 unset ($haohappy 2); Delete reference: Reference count = 0 automatically frees up memory space?