PHP5 Learning Notes
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, the interdependence is too strong. The change of a module causes the other modules to be changed as well.
Imagine if there was a module for handling logins that promised 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, and it may sever the protocol between the two. The processing of the credit card module resulted in an error. 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 dependence 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 are 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, providing 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 difficult to grasp at first, but I can assure you that once you grasp it, it will be very natural to think with its thinking.
Section II--PHP5 object model
PHP5 has a single resume that restricts access to the object model that can be overloaded. Later in this chapter, the "Continue" is specifically discussed, containing the parent-child relationships between classes. In addition, PHP supports restrictive access to properties and methods. You can declare members as private and do not allow access to external classes. Finally, PHP promises a subclass to overload members from its parent class.
Haohappy Note: There is no private in PHP4, only public.private is good for better implementation of encapsulation.
The PHP5 object model considers objects to be passed by reference as distinct from any other data type. PHP does not require you to pass and return objects explicitly by reference (reference). The object model based on the handle will be specifically elaborated at the end of this chapter. It is the most important new feature in PHP5.
With a more direct object model, a handle based system has additional advantages: increased efficiency, less memory footprint, and greater flexibility.
In the first few versions of PHP, the script replicates objects by default. Now PHP5 only moves the handle, which takes less time. The improvement in script execution efficiency is due to the avoidance of unnecessary replication. While the object system brings complexity, it also brings the benefit of execution efficiency. At the same time, reducing replication means taking in less memory, allowing more memory to be left to other operations, which also increases efficiency.
Haohappy Note: Based on the handle, that is, two objects can point to the same memory, both reduce the copy action, and reduce the memory footprint.
The Zand Engine 2 has greater flexibility. An exciting development is the promise of deconstruction--Executing a class method before the object is destroyed. This is also good for using memory, so that PHP clearly knows when there are no references to objects and allocates the empty memory to other uses.
Section III-Defining a class
When you declare a class, you need to list all the variables and all the functions that the object should have.