Object-oriented programming is designed to provide solutions for large software projects, especially projects involving multiple people. When the source code grows to 10,000 lines or more, every change may cause undesirable side effects. This 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 accordingly.
Imagine if a module that handles logins allows a credit card processing module to share its database connection. Of course the intention is good, saving the expense of making another database connection. However, sometimes, the login processing module changes one of the database connections. The name of the variable may sever the agreement between the two, causing 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 make errors due to this.
Therefore, I feel a bit Dramatically, most programmers are grateful for coupling and encapsulation. Coupling is a measure of the degree of dependence between two modules. The less coupling the better. We want to be able to take a module out of an existing project and use it in another Used 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 data communication between modules Do it through interfaces. Modules don't snoop through each other's variable names, they politely send requests through functions.
Encapsulation is a principle you can use in any programming language. In PHP and many procedural-oriented 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 point of view, methods are functions and properties are variables. In In an ideal object-oriented system, each part is an object. The system consists of objects and the relationships between objects through methods.
A class defines the properties of the object. If you are baking a set of cookie objects , then the class will be a cookie machine. The properties and methods of the class are called members. One can express this by saying data members or method members.
Each language provides different ways to access objects. PHP comes from C++ Borrowing concepts, providing 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-scale projects exceeding 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 master it, thinking with its mind will feel very natural.
PHP5's object model
PHP5 has a single inheritance, restricted access, and overloadable object model. "Inheritance", which will be discussed in detail later in this chapter, includes parent-child relationships between classes. In addition, PHP supports attributes and Restricted access to methods. You can declare members as private, disallowing access from outside classes. Finally, PHP allows a subclass to overload members from its parent class.
PHP5's object model treats objects differently from any other data type , passed by reference. PHP does not require you to explicitly pass and return objects by reference. The reference-based object model will be explained in detail at the end of this chapter. It is the most important new feature in PHP5.
It is more direct The object model has additional advantages: increased efficiency, less memory usage, and greater flexibility.
In previous versions of PHP, scripts copied objects by default. Now PHP5 only moves handles, requiring less Time. The improvement in script execution efficiency is due to the avoidance of unnecessary copying. While the object system brings complexity, it also brings benefits in execution efficiency. At the same time, reducing copying means that less memory is occupied, which can be reserved This frees up more memory for other operations, which also improves efficiency.
Zand Engine 2 has greater flexibility. A happy development is to allow destruction-executing a class method before the object is destroyed. This is also good for utilizing memory. It is very beneficial, allowing PHP to clearly know when there is no reference to the object, and allocate the free memory to other uses.
PHP5's memory management
object transfer
PHP5 uses the Zend Engine II, and the objects are stored in a separate structure Object Store. It is not stored in Zval like other general variables (in PHP4, objects are stored in Zval like general variables). Only the pointer of the object is stored in Zval rather than the content (value). When we copy an object or pass an object as a parameter to a function, we do not need to copy the data. Just keep the same object pointer and notify the Object Store that this particular object now points to via another zval. Since the object itself is located in the Object Store, any changes we make to it will affect all zval structures holding pointers to the object - manifested in the program as any changes to the target object will affect the source object. .This makes PHP objects look like they are always passed by reference, so objects in PHP are passed by "reference" by default and you no longer need to use & to declare them like in PHP4.
Garbage collection mechanisms
in some languages, most typically C, require you to explicitly ask for memory allocation when you create a data structure. Once you allocate memory, you can store information in variables. At the same time, you also need to release the memory when you are done using the variable, so that the machine can free up memory for other variables and avoid running out of memory.
PHP can automatically manage memory and clear objects that are no longer needed. PHP uses a simple garbage collection mechanism called reference counting. Each object contains a reference counter, and each reference connected to the object increases the counter by one. When reference leaves the living space or is set to NULL, the counter is decremented by 1. When an object's reference counter reaches zero, PHP knows that you no longer need to use the object and releases the memory space it occupies.
For example:
<?php
class Person{}
function sendEmailTo(){}
$haohappy = new Person( );
// Create a new object: Reference count Reference count = 1
$haohappy2 = $haohappy;
// Copy by reference: Reference count = 2
unset($haohappy);
// Delete a reference: Reference count = 1
sendEmailTo($haohappy2);
// Pass object by reference:
// During function execution:
// Reference count = 2
//After execution:
// Reference count = 1
unset($haohappy2);
// Delete reference: Reference count = 0 Automatically release memory space
?>