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.
Note: metaphor refers to the relationship between modules is too high, mutual dependence 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.
Note: There is no private in PHP4, only public.private is good for better encapsulation.
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). In the end of this chapter, the object model based on the handle is elaborated in detail. It is the most important new feature in PHP5.
With a more straightforward object model, a handle-based architecture has additional advantages: increased efficiency, low memory footprint, 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.
Note: based on a handle, it means that two objects can point to the same piece of memory, reducing both the copy action and the memory footprint.
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.
http://www.bkjia.com/PHPjc/446723.html www.bkjia.com true http://www.bkjia.com/PHPjc/446723.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 ...