Home >Backend Development >PHP Tutorial >Classes and Objects in PHP5-Object-oriented programming [1]_PHP tutorial
Author: Leon Atkinson Translated by: Haohappy
Object-oriented programming is designed to provide solutions for large software projects, especially projects involving multiple people. When the source code grows to 10,000 lines or more, every change may cause undesirable side effects. This The situation occurs when modules form secret alliances, just like Europe before World War I.
//haohappy Note: It means that the correlation between modules is too high and the interdependence is too strong. Changing one module requires that other modules must also be changed.
Imagine if a module that handles login allows a credit card processing module to share its database connection. Of course, the starting point is good, saving money In order to make another database connection, however, sometimes, the login processing module changes the name of one of the variables, which may sever the agreement between the two, causing processing errors in the credit card module, which in turn leads to errors in the invoice processing module. Soon Therefore, I think it is a bit dramatic that most programmers are grateful for coupling and encapsulation. Coupling is the degree of dependence between two modules The less coupling, the better. We hope to be able to take a module from an existing project and use it in another new project.
We also hope to make large-scale changes within a module Without worrying about the impact on other modules. The principle of encapsulation can provide this solution. Modules are treated as relatively independent, and data communication between modules is carried out through interfaces. Modules do not peek into another module through each other's variable names, they Politely send requests through functions.
Encapsulation is a principle you can use in any programming language. In PHP and many procedural-oriented languages, it's tempting to be lazy. There's nothing to stop it You build a hypothetical WEB through modules. Object-oriented programming is a way for programmers to not violate the principle of encapsulation.
In object-oriented programming, modules are organized into objects. These objects have methods and properties. From an abstract perspective, methods are the actions an object performs, while properties are the characteristics of the object. From a programming perspective, methods are functions and properties are variables. In an idealized world In an object-oriented system, each part is an object. The system is composed of objects and the relationships between objects through methods.
A class defines the properties of the object. If you are baking a set of sweets pie object, then the class will be a cookie machine. The properties and methods of the class are called members. One can express it by saying the data members or method members.
Each language provides Different ways to access objects. PHP borrows concepts from C++ and provides a data type to contain functions and variables under an identifier. When PHP was originally designed, and even when PHP3 was developed, PHP was not intended to provide the ability to develop large-scale projects exceeding 100,000 lines of code. With the development of PHP and Zend engine, it has become possible to develop large projects, but no matter how big your project is, writing your scripts in classes will allow code reuse. This is a good idea, especially if 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 hard to master it at first, but I can guarantee that once you master it, thinking with its mind will feel very natural.
http://www.bkjia.com/PHPjc/314098.html