Home  >  Article  >  Backend Development  >  Section 1 - Object-oriented Programming_PHP Tutorial

Section 1 - Object-oriented Programming_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 16:01:10812browse

+------------------------------------------------- --------------------+
| = This article is read by Haohappy<>
| = Classes and Objects 1 Chapter's notes
| = Translation + personal experience
| = To avoid possible unnecessary trouble, please do not reprint, thank you
| = We welcome criticisms and corrections, and hope to make progress together with all PHP enthusiasts !
| = PHP5 Research Center: http://blog.csdn.net/haohappy2004
+------------------------- --------------------------------------------------+
*/

Section 1 - Object-oriented programming

Object-oriented programming is designed to provide solutions for large-scale software projects, especially projects involving multiple people. When the source code grows to 10,000 lines Even more often, every change can lead to undesirable side effects. This happens when modules form secret alliances, like in 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 will cause other modules to also be changed.

Imagine if there is a module used to handle login The module allows a credit card processing module to share its database connection. Of course, the intention is good, saving the expense of another database connection. However, sometimes, the login processing module changes the name of one of the variables, which may cut off the connection between the two. protocol. This leads to an error in the processing of the credit card module, which in turn leads to an error in the module that processes invoices. Soon, all irrelevant modules in the system may error from this.

Therefore, I think it is a bit dramatic, absolutely Most programmers are grateful for coupling and encapsulation. Coupling is a measure of the dependence between two modules. The less coupling the better. We want to be able to take a module from an existing project and use it in a new project .

We also hope to make large-scale changes within a certain module without worrying about the impact on other modules. The principle of encapsulation can provide this solution. Modules are regarded as relatively independent, and the data between modules Communication happens through interfaces. Modules don't peek into each other's variable names, they politely send requests through functions.

Encapsulation is a principle you can use in any programming language. In PHP As with many procedural languages, it's tempting to be lazy. There's nothing stopping you from building 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 point of view, methods are the actions of an object, and properties are the characteristics of the object. .From a programming perspective, methods are functions and attributes are variables. In an ideal 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 an object. If you were baking a set of cookie objects, the class would be the cookie maker. The properties and methods of the class are the members that are called. One can specify the data members by saying Or method members to express.


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 projects with more than 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 do, thinking with its mind will feel very natural.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/316963.htmlTechArticle+----------------------- --------------------------------------------------+ |=This article Read CorePHP Programming for Haohappy |=Notes from the ClassesandObjects chapter|=Translation + personal experience|=...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn