PHP5 Object-Oriented Programming _php tutorial

Source: Internet
Author: User
PHP5 has a single-re-inherited, restricted-access, object model that can be overloaded. The "Inheritance", which is discussed in detail later in this chapter, contains parent-child relationships between classes. 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 PHP5 the object as if it were to be passed by reference, unlike any other data type. 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, the script replicates the object by default. Now PHP5 only moves the handle, requiring 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:

Copy CodeThe code is as follows:
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);
To pass an object by reference:
During function execution:
Reference count = 2
When execution is complete:
Reference count = 1

Unset ($haohappy 2);
Delete reference: Reference count = 0 automatically frees memory space

?>

http://www.bkjia.com/PHPjc/318700.html www.bkjia.com true http://www.bkjia.com/PHPjc/318700.html techarticle PHP5 has a single-inherited, restricted-access object model that can be overloaded. "Inheritance", which is discussed in detail later in this chapter, contains parent-child relationships between classes. In addition, PHP supports the limitation of properties and methods ...

  • 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.