Implement object-oriented programming in PHP (below ). Polymorphism is defined as the ability of an object to call a method when it is passed as a parameter at runtime. For example, a class defines the method draw and inherits class polymorphism.
Polymorphism is defined as the ability of an object to call a method when it is passed as a parameter at runtime. For example, if you use a class to define the method "draw" and inherit the class to redefine the behavior of "draw" to draw circles or squares, then you have a function with the parameter x, you can call $ x-> draw () in the function (). 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 in the C ++ compiler if it is compiled, that method is called? However, you don't know what the object type 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); // will call the draw method of Rectangle.
?>
PHP object-oriented programming
It is true that PHP is not a real object-oriented language. PHP is a hybrid language that you can use in object-oriented or traditional structured programming methods. For large projects, however, you may or need to use a pure object-oriented method to define classes and use only objects and classes in your project. More and more large projects will benefit from the use of object-oriented methods. object-oriented projects are very easy to maintain, easy to understand and reuse. This is the basis of software engineering. Using these concepts is the key to future success in website design.
Advanced object-oriented technology in PHP
After reviewing the basic concepts of object-oriented, I will introduce some more advanced technologies.
Serializing
PHP does not support persistent objects. in object-oriented languages, persistent objects are some objects that remain in the state and function after multiple calls by applications, this means that an object can be saved to a file or database and then reloaded. This mechanism is called serialization. PHP has a serialization function that can be called in an object. the serialization function returns a string representing this object. The serialized function then stores the member data rather than the member function.
In PHP4, if you serialize an object to the string $ s, delete the object, and deserialize the object to $ obj, you can still call the method function of the object. But I do not recommend this method, because (a) This feature will not necessarily support (B) in the future, which leads to an illusion if you save the serialized object to the disk and exit the program. When you re-run the script in the future, you cannot deserialize the object and expect that the method function of the object will still be valid, because the serialized string does not represent any member function. Finally, serialized member variables for saving objects are very useful in PHP (you can concatenate arrays and arrays into the disk ).
The sequence polymorphism is defined as the ability of an object to call a method when it is passed as a parameter at runtime. For example, a class is used to define the method "draw" to inherit the class...