This article introduces application examples of automatic loading classes, object serialization, and polymorphic applications in object-oriented PHP. Students who need to know more can take a look.
Auto-loading classes
When many developers write object-oriented applications, they create a PHP source file for each class definition. A big annoyance is having to write a long list of include files at the beginning of each script (one file per class).
In a software development system, it is impossible to write all classes in a PHP file. When a PHP file needs to call a class declared in another file, it needs to be included through include. File import. However, sometimes, in projects with many files, it is a headache to include all the files of the required classes one by one. So, can we include this class when it is used? Where to import the php file? This is the autoloading class we are going to talk about here.
In PHP 5, you can define an __autoload() function, which will be automatically called when trying to use a class that has not yet been defined. By calling this function, the script engine has the last one before PHP fails with an error. Opportunity to load the required class. One parameter received by the __autoload() function is the class name of the class you want to load. So when you are working on a project, you need to follow certain rules when organizing the file names of the defined classes. It is best to use The class name is the center. You can also add a unified prefix or suffix to form the file name, such as xxx_classname.php, classname_xxx.php and just classname.php, etc.
In this example, try to start from MyClass1.php and MyClass2 respectively. Load the MyClass1 and MyClass2 classes in the .php file
The code is as follows | Copy code | ||||
|
Object serialization
Sometimes it is necessary to transmit an object over the network. In order to facilitate transmission, the entire object can be converted into a binary string, and when it reaches the other end, Restore to the original object, this process is called serialization, just like we want to transport a car to the United States by ship now, because the car is relatively large, we can disassemble the car into small parts, and then We ship these parts to the United States by wheel, and then assemble these parts back into the car.
There are two cases where we must serialize the object. The first case is to serialize the object when transmitting it on the network. The second case is to write the object to a file or Serialization is used when it is a database.
There are two processes in serialization. One is serialization, which is to convert the object into a binary string. We use the serialize() function to serialize an object, and the other is deserialization. , which is to convert the object-converted binary string into an object. We use the unserialize() function to deserialize an object.
The parameter of the serialize() function in PHP is the object name, and the return value is a String, the string returned by Serialize() has ambiguous meaning. Generally, we will not parse this string to get the object information. We only need to pass the returned string to the other end of the network or save it in the component.
The unserialize() function in PHP is used to deserialize objects. The parameter of this function is the return value of the serialize() function. The output is of course the reorganized object.
The code is as follows | Copy code td> | ||||
|
Application of polymorphism
Polymorphism is one of the three major characteristics of object-oriented objects besides encapsulation and inheritance. In my personal opinion, although polymorphism can be achieved in PHP, But compared with object-oriented languages such as C++ and Java, polymorphism is not so prominent, because PHP itself is a weakly typed language, and there is no conversion of parent class objects into subclass objects or subclass objects into The problem of parent class objects, so the application of polymorphism is not so obvious; the so-called polymorphism refers to the ability of a program to handle multiple types of objects. For example, when working in a company, the financial department pays wages every month, and the same payer Wages are paid to different employees or employees in different positions within the company through this method, but the wages paid are all different. Therefore, the same method of paying wages appears in many forms. For object-oriented programs, polymorphism is to assign the subclass object to the parent class reference, and then call the parent class method to execute the method of the subclass overriding the parent class, but in PHP it is weakly typed, and the object reference They are all the same regardless of parent class reference or subclass reference.
Let’s look at an example now. First of all, to use polymorphism, there must be a relationship between parent class objects and subclass objects. Make a shape interface or abstract class as the parent class. There are two abstract methods in it, one is to find the perimeter, and the other is to find the area. The subclasses of this interface are a variety of different shapes, each Shapes have perimeter and area, and because the parent class is an interface, the subclass must implement the two abstract methods of perimeter and area of the parent class. The purpose of this is to make the subclass of each different shape All classes comply with the specifications of the parent class interface and have methods for calculating perimeter and area.
The code is as follows
|
Copy code
|
||||
//Defines a shape interface, which has two abstract methods for subclasses to implement |