PHP5 has a single-inheritance, restricted access to the object model that can be overloaded. "Inheritance", which is discussed in detail later in this chapter, contains the parent-child relationships between classes. In addition, PHP supports restrictive access to properties and methods. You can declare that members are private and do not allow external class access. Finally, PHP allows a subclass to overload members from its parent class.
The PHP5 object model considers objects to be passed by reference as distinct from any other data type. PHP does not require you to pass and return objects explicitly by reference (reference). The reference-based object model will be elaborated 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 footprint, and greater flexibility.
In the first few versions of PHP, the script replicates objects 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 replication. While the object system brings complexity, it also brings the benefit of execution efficiency. At the same time, reducing replication means taking in less memory, allowing more memory to be left to other operations, which also increases efficiency.
The Zand Engine 2 has greater flexibility. One pleasing development is allowing for deconstruction--Executing a class method before the object is destroyed. This is also good for using memory, so that PHP clearly knows when there are no references to objects and allocates the empty memory to other uses.
Add:
Memory management for PHP5
Object delivery
PHP5 uses the Zend Engine II, which is stored in a separate structure object store, rather than stored in zval like other general variables (the object is stored in the zval as the generic variable in PHP4). Only the object's pointer, not the content (value), is stored in the zval. When we copy an object or pass an object as an argument to a function, we do not need to copy the data. Just keep the same object pointer and be notified by another zval that this particular object is now pointing to the object Store. Since the object itself is located in the object Store, any changes we make to it will affect all the zval constructs that hold the object's pointer----performance in the program is that any changes to the target object will affect the source object. This makes the PHP object look like always by reference (reference To pass, so the object in PHP is passed by reference, and you no longer need to use & to declare it as in PHP4.
Garbage collection mechanism
Some languages, most typically like C, require you to explicitly require allocating memory when you create data structures. Once you have allocated the memory, you can store the information in the variable. You also need to free up memory at the end of the use of variables, which allows the machine to empty out memory to other variables to avoid consuming memory.
PHP can automate memory management and clear out objects that are no longer needed. PHP uses the simple garbage collection (garbage collection) mechanism of reference counting (reference counting). Each object contains a reference counter, each reference is connected to the object, and the counter is added 1. When reference leaves the living space or is set to NULL, the counter is reduced by 1. When an object's reference counter is zero, PHP knows that you will no longer need to use this object to free up the memory space it occupies.
For example:
Copy Code code as follows:
<?php
Class person{
}
function Sendemailto () {
}
$haohappy = new Person ();
Create a new object: reference count Reference count = 1
$haohappy 2 = $haohappy;
Copying 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 is completed:
Reference count = 1
Unset ($haohappy 2);
Delete reference: Reference count = 0 Auto free memory space
?>