Home > Article > Backend Development > Implementing object-oriented programming in PHP (Part 2)_PHP tutorial
Polymorphism
Polymorphism is defined as the ability of an object to decide which method to call when an object is passed as a parameter at run time. For example, a class defines the method "draw", and the inherited class redefines the behavior of "draw" to draw a circle or a square, so that you have a function with a parameter of x, in which you can call $x->draw() . If polymorphism is supported, the call to the "draw" method depends on the type of object x. Polymorphism is naturally supported in PHP (think about this situation if compiled in a C++ compiler, which method is called? However, you don't know what the type of the object is, of course this is not the case now).
Fortunately, PHP supports polymorphism.
function niceDrawing($x) {
//Supose this is a method of the class Board.
$x->draw();
}
$obj=new Circle(3,187 );
$obj2=new Rectangle(4,5);
$board->niceDrawing($obj); //will call the draw method of Circle.
$board->niceDrawing($obj2 ; PHP is a hybrid language, you can use it with object-oriented or traditional structural programming methods. For large projects, however, you may or need to use a pure object-oriented approach to defining classes and use only objects and classes in your project. Larger and larger projects will benefit from using an object-oriented approach. Object-oriented projects are easier to maintain, easier to understand, and easier to reuse. This is the basis of software engineering. Using these concepts in website design is the key to future success.
Advanced object-oriented techniques in PHP
After reviewing the basic concepts of object-oriented, I will introduce some more advanced techniques.
Serialization
PHP does not support persistent objects. In object-oriented languages, persistent objects are objects that retain their state and functionality after being called multiple times by the application. This means that there is a way to save the object. to a file or database and then reload the object. This mechanism is called serialization. PHP has a serialization function that can be called on an object. The serialization function returns a string representing the object. Then the serialization function saves the member data instead of the member function.
In PHP4, if you serialize an object to the string $s, then delete the object, and then deserialize the object to $obj, you can still call the object's method function. But I don't recommend this method because (a) this feature may not be supported in the future and (b) it leads to a phantom if you save the serialized object to disk and exit the program. You cannot deserialize this object and expect the object's methods to still be valid when rerunning this script in the future, because the serialized string does not represent any member functions. Finally, serializing member variables of saved objects is very useful in PHP, and that's all. (You can serialize associative arrays and arrays to disk).