Section I-object-oriented programming-Classes and Objects in PHP5 [1]

Source: Internet
Author: User
Tags functions variables php and php programming variable
object|php5| Programming | objects | = This article is for Haohappy read <<core PHP programming>>
| = Notes from the chapter classes and objects
| = translation-oriented + personal experience
| = Please do not reprint to avoid any unnecessary trouble that may occur, thank you
| = Welcome to criticize, hope and all PHP enthusiasts to progress together!
+---------------------------------------------------------------------+
*/

The first section-object-oriented programming

Object-oriented programming is designed to provide solutions for large software projects, especially for people who collaborate on multiple projects. When the source code grows to 10,000 lines or more, every change can lead to unwanted side effects. This happens when a secret alliance is formed between modules, like Europe before the First World War.

Haohappy Note: Metaphor refers to the relationship between the module is too high, mutual dependence is too strong. The change of a module causes 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 and saves the expense of making another database connection. However, sometimes the login processing module changes the name of one of the variables, which may sever the protocol between the two. The processing of the credit card module resulted in an error in the module processing the invoice. Soon, all the unrelated modules in the system could be wrong.

So I think it's a bit dramatic that most programmers are thankful for coupling and encapsulation. Coupling is a measure of the degree of dependency between two modules. The less the coupling, the better. We want to be able to take a module from an existing project and use it in another new project.

We also want large-scale changes within a module without worrying about the impact on other modules. The principle of encapsulation can provide this solution. The module is regarded as relatively independent, and the data communication between the modules is carried out through the interface. Modules do not pry through each other's variable names to spy on another module, which sends requests politely through functions.

Encapsulation is a principle that you can use in any programming language. In PHP and many process-oriented languages, it's 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 encapsulation principles.


In object oriented programming, modules are organized into objects. These objects have methods and properties. From an abstract standpoint, a method is the action of an object, and a property is an attribute of an object. From a programmatic standpoint, 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 by means of the method.

A class defines the properties of an object. If you're baking a group of cookie objects, then the class will be the cookie machine. The properties and methods of a class are called members. One can express by saying the data member or the method member.


Each language provides a different way to access the object. PHP borrows concepts from C + + and provides 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 projects, but no matter how big your project is, writing your scripts with 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 of objects is one of the most exciting concepts in computer science. It's hard to start with, but I can assure you that once you have mastered it, it will be natural to think with its thinking.

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.