The beginning of depth exploration of PHP5.0 object model

Source: Internet
Author: User
Tags data structures garbage collection object model php and zend

Object-oriented programming is designed to provide solutions for large software projects, especially for people who collaborate on multiple projects. When the source code grows to 10,000 lines or more, every change can lead to unwanted side effects. This happens when a secret alliance is formed between modules, like Europe before the First World War.

Haohappy Note: Metaphor refers to the relationship between the module is too high, the interdependence is too strong. The change of a module causes the other modules to be changed as well.

Imagine if there was a module for handling logins that promised a credit card processing module to share its database connection. Of course, the starting point is good and saves the expense of making another database connection. However, sometimes the login processing module changes the name of one of the variables, and it may sever the protocol between the two. The processing of the credit card module resulted in an error. Soon, all the unrelated modules in the system could be wrong.

So I think it's a bit dramatic that most programmers are thankful for coupling and encapsulation. Coupling is a measure of the degree of dependence between two modules. The less the coupling, the better. We want to be able to take a module from an existing project and use it in another new project.

We also want large-scale changes within a module without worrying about the impact on other modules. The principle of encapsulation can provide this solution. The module is regarded as relatively independent, and the data communication between the modules is carried out through the interface. Modules do not pry through each other's variable names to spy on another module, which sends requests politely through functions.

Encapsulation is a principle that you can use in any programming language. In PHP and many process-oriented languages, it's tempting to be lazy. Nothing can stop you from building an imaginary web through a module. Object-oriented programming is a way to make programmers not violate encapsulation principles.

In object oriented programming, modules are organized into objects. These objects have methods and properties. From an abstract standpoint, a method is the action of an object, and a property is an attribute of an object. From a programmatic standpoint, a method is a function and a property is a variable. In an idealized object-oriented system, each part is an object. The system is formed by the relation between the object and the object by means of the method.

A class defines the properties of an object. If you are baking a group of cookie objects, then the class will be the cookie machine. The properties and methods of a class are called members. One can express by saying the data member or the method member.

Each language provides a different way to access the object. PHP borrows concepts from C, providing a data type to contain functions and variables under an identifier. When PHP was originally designed, even when PHP3 was developed, PHP did not intend to provide the ability to develop large projects with more than 100,000 lines of code. With the development of PHP and Zend engines, it is possible to develop large projects, but no matter how big your project is, writing your scripts with classes will allow code to be reused. This is a good idea, especially when you are willing to share your code with others.

The idea of objects is one of the most exciting concepts in computer science. It's difficult to grasp at first, but I can assure you that once you grasp it, it will be very natural to think with its thinking.

The object model of PHP5

PHP5 has a single resume that restricts access to the object model that can be overloaded. Later in this chapter, the "Continue" is specifically discussed, containing the parent-child relationships between classes. In addition, PHP supports restrictive access to properties and methods. You can declare members as private and do not allow access to external classes. Finally, PHP promises 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 object model based on references will be specifically 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. An exciting development is the promise of 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.

Memory governance 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 governance 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:

<?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
?>

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.