Classes and Objects in PHP5-Object-Oriented Programming [1]

Source: Internet
Author: User


Author: Leon Atkinson Translation: Haohappy object-oriented programming is designed to provide solutions for large-scale software projects, especially projects that involve collaboration among multiple people. when the source code grows to 10 thousand lines or even more lines, every change may lead to unwanted side effects. this happens when modules form a secret alliance, just like Europe before the First World War.

// Haohappy Note: The correlation between modules is too high and the dependency between modules is too strong. If a module is changed, other modules must also change.

Imagine if a login processing module allows a credit card processing module to share its database connection. of course, the starting point is good, saving money for connecting to another database. however, sometimes, when the login processing module changes the name of a variable, the protocol between the two may be broken. this leads to an error in the processing of the credit card module, which leads to an error in the processing of the invoice module. soon, all irrelevant modules in the system may fail.

Therefore, I think it is a little dramatic that most programmers are grateful for coupling and encapsulation. coupling is a measure of the degree of dependency between two modules. the less coupling, the better. we hope to extract 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 encapsulation principle can provide this solution. modules are regarded as relatively independent, and data communication between modules is performed through interfaces. modules do not use their variable names to snoop on another module. They use functions to send requests politely.

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 prevent you from using modules to build a hypothetical WEB. object-Oriented programming is a method that prevents programmers from violating encapsulation principles.


In object-oriented programming, modules are organized into objects. these objects have methods and attributes. from an abstract point of view, a method is an action of an object, and a property is a feature of an object. from the programming point of view, the method is a function and the attribute is a variable. in an idealized object-oriented system, each part is an object. the system consists of links between objects and objects through methods.

A class defines the attributes of an object. if you are baking a set of cookies, the class will be a cookie machine. class attributes and methods are called members. people can express data members or method members.


Each language provides a different way to access objects. PHP borrowed concepts from C ++ and provided a data type to include functions and variables under an identifier. When PHP was initially designed and even when PHP3 was developed, PHP was not intended to provide the ability to develop large projects with over 0.1 million lines of code. With the development of PHP and Zend engines, it is possible to develop large projects. However, no matter how large your project is, writing your scripts using classes will allow code to be reused. This is a good idea, especially when you are willing to share your code with others.

The idea about objects is one of the most exciting concepts in computer science. It is hard to grasp it at first, but I can assure that once you master it, it will be very natural to think with it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.