PHP object-oriented programming _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
PHP object-oriented programming. Object-oriented programming is designed to provide solutions for large-scale software projects, especially projects with multi-person cooperation. when the source code grows to 10 thousand lines or even more lines, every change may lead to object-oriented programming being designed to provide solutions for large software projects, especially projects that involve multiple partners. 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.

Note:It means that the correlation between modules is too high and the mutual dependence 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.

PHP5 object model

PHP5 has a single-inherited, restricted-access, and reload object model. this chapter will discuss inheritance in detail later, including the parent-child relationship between classes. in addition, PHP supports restricted access to attributes and methods. you can declare that the members are private and do not allow external Class access. finally, PHP allows a subclass to reload members from its parent class.

Note:PHP4 does not have private, and only public. private is good for better encapsulation.

The object model of PHP5 treats an object as different from any other data type and is passed through reference. PHP does not require you to pass and return objects through reference explicitly. at the end of this chapter, we will elaborate on the handle-based object model. it is the most important new feature in PHP5.

With a more direct object model, the handle-based system has the following advantages: higher efficiency, less memory occupation, and greater flexibility.

In the first few versions of PHP, the script copies objects by default. now, PHP5 only moves the handle, which takes less time. the script execution efficiency is improved because unnecessary copying is avoided. while the object system brings complexity, it also brings the benefit of execution efficiency. at the same time, reducing replication means that less memory is occupied and more memory can be set aside for other operations, which also improves the efficiency.

Note:Based on the handle, the two objects can point to the same memory, which reduces the replication action and memory usage.

Zand engine 2 has greater flexibility. a happy development is to allow the destructor to execute a class method before the object is destroyed. this is also very good for the use of memory, so that PHP can clearly know when there is no object reference, and allocate the empty memory to other purposes.


Pipeline. when the source code increases to 10 thousand lines or even more lines, every change may result in...

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.