Section 1 - Object-Oriented Programming - Classes and Objects in PHP5 [1]
Author: Leon Atkinson translation: Haohappy Source: Beyond the PHP / * ------------------------------------ --------------------------------- | = This article is Haohappy read << Core PHP programming >> | = Classes and Objects Chapter of the note | = Translate main personal experience | = To avoid unnecessary troubles, please do not reprint, thank you | = Welcome criticism, hope to make progress together with all PHP enthusiasts! - -------------------------------------------------- ---------------- * / Section - Object-oriented programming object-oriented programming is designed to provide solutions for large software projects, especially multi-person cooperation. The source code increases to 10,000 lines and even more, each more can lead to undesirable side effects. This situation occurs when the module has formed a secret alliance, just like the first World War. Europe. // haohappy Note: The correlation between the module is high, the mutual dependence is too strong. Moving a module causing other modules to be more moving. Imagine, if there is a module for processing login allows a credit card processing The module is to share its database connection. Of course, the starting point is good, saving the expenditure of another database connection. However, sometimes the login processing module changes the name of one of the variables, it may cut off the agreement between the two. Lead to the credit card The module's handling error, which causes the module that processes the invoice error. Soon, all unrelated modules in the system may be wrong. So I feel a bit dramatic, most of the programmers are grateful to coupling and packaging Coupling is a measure of two modular dependencies. The less coupling, the better. We hope to take a module from existing projects and use them in another new project. We also hope to have a large scale in a certain module. Different without worry about other modules. The principle of packaging can provide this solution. The module is viewed to be relatively independent, and the data communication between the modules is performed by an interface. The module does not detect another one by the variable name of each other. Modules, they politely send requests through functions. Package is a principle you can use in any programming language. In PHP and many process-oriented languages, it is very tempting. Nothing can stop you from passing by module To build a virtual web. Object-oriented programming is a method that makes programmers will not violate the package principles. In object-oriented programming, the module is organized into one object. These objects have methods and properties. From abstract angle Look, the method is one The action of an object, and the attribute is the characteristics of the object. From the programming point of view, the method is the function and the attribute is a variable. In an ideal object-oriented system, each part is an object. System The contacts formed by the object and the object are formed. A class defines the properties of an object. If you are bake a set of sweet cake objects, then the class will be a sweet cake machine. Class properties and methods are members called It can be expressed by saying data members or methods. Each language provides different ways to access objects. PHP borrows concepts from C , providing a data type to include functions and variables under an identifier. When I originally designed PHP, even PHP3 was developed, PHP did not intend to provide the ability to develop large projects that exceed 100,000 lines of code. With the development of PHP and Zend Engine, it is possible to develop large projects, but no matter how much your project is large, write your script with classes to make the code reuse. 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 of computer science. It is difficult to master it, but I can guarantee that once you have mastered it, it will be very nature to use its thinking.