Object-oriented programming is designed to provide solutions for large software projects, especially for multi-person collaboration projects. When the source code grows to 10,000 lines or more, every change can lead to undesirable side effects. This happens when a secret alliance is formed between modules, like Europe before the First World War.
Haohappy Note: The correlation between the modules is too high and the interdependence is too strong. The change of one module causes the other modules to be changed as well.
Imagine if there is a module for handling logins that allows a credit card processing module to share its database connection. Of course, the starting point is good, saving the expense of making another database connection. However, sometimes, the login processing module changes the name of one of the variables, it may have severed the agreement between the two. The processing of the credit card module failed, resulting in an error in the module processing the invoice. Quickly, all unrelated modules in the system can be faulted.
So, I think it's a little dramatic that most programmers are thankful for coupling and encapsulation. Coupling is a measure of the degree of dependency between two modules. The less coupling the better. We want to be able to take one module from an existing project and use it in another new project.
We also want to have large-scale changes within a module without worrying about the impact on other modules. The principle of encapsulation can provide this solution. The modules are regarded as relatively independent, and the data communication between the modules is carried out through the interface. Modules do not spy on another module by their variable names, they use functions to politely send requests.
Encapsulation is a principle that you can use in any programming language. In PHP and many process-oriented languages, it is 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 the encapsulation principle.
In object-oriented programming, modules are organized into objects. These objects have methods and properties. From an abstract point of view, a method is an action made by an object, and a property is an attribute of an object. From a programmatic point of view, 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 through the method.
A class defines the properties of an object. If you bake a group of cookie objects, the class will be the cookie machine. The properties and methods of the class are called members. People can express themselves by saying data members or method members.
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-scale projects, but no matter how large your project is, writing your scripts with classes will allow the 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 is difficult to master it at first, but I can assure you that once you have mastered it, it will be very natural to think with it.
Object model of PHP5
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.
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:
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/508377.html www.bkjia.com true http://www.bkjia.com/PHPjc/508377.html techarticle Object-oriented programming is designed to provide solutions for large software projects, especially for multi-person projects. When the source code grows to 10,000 lines or more, every change can lead to ...