PHP5 polymorphism and dynamic binding method
Polymorphism is the third feature of object-oriented language following the abstraction and inheritance of data. Literally, polymorphism means "multiple forms", and in simple terms, polymorphism is a feature that has the ability to express many forms, in Oo, meaning "language has the ability to deal with different ways depending on the type of object, especially overloaded methods and inherited classes." Polymorphism is considered an essential feature of object-oriented languages.
For example:
Create an interface Shape, define an empty method draw (), then all the implementation classes must implement this method, assuming that Shape has two implementation classes: Triangle and Rectangle, although we cannot interpret PHP polymorphism with Java code like this:
Shape s = new Triangle ();
S.draw ();
Class Testpolymorphism {
Public Function Drawnow (Shape $shape) {
$shape->draw ();
}
}//codego.net
The function Drawnow () restricts the incoming parameter type to be an object of the Shape interface derived class, where the arguments we pass to Drawnow () may be Triangle or Rectangle objects, or other derived class objects of the shape interface, such as Cir CLE and so on, simply saying that the Drawnow () parameter type is unpredictable, and that the behavior of the $shape->draw () is ultimately determined by the specific type of parameters passed in, such as if the Triangle object is passed in, then Triangle's draw () method is called, If the Rectangle object is passed in, the Rectangle draw () method is called. The behavior of a method that determines which object to invoke depends on the type of object parameter passed in at run time, which can be called polymorphic.
Shape can also be an abstract base class or a non-abstract base class, and the above discussion is all set up. The difference is that the interface defines only a set of rules that an implementation class must follow, while using a base class can provide some default behavior for derived classes.
/**
* Shape Interface
*
* @version 1.0
* @copyright
*/
Interface Shape {
Public function Draw ();
}
/**
* Triangle
*
* @uses Shape
* @version 1.0
* @copyright
*/
Class Triangle implements Shape {
Public Function Draw () {
Print "Triangle::d raw () \ n";
}
}
/**
* Rectangle
*
* @uses Shape
* @version 1.0
* @copyright
*/
Class Rectangle implements Shape {
Public Function Draw () {
Print "Rectangle::d raw () \ n";
}
}
/**
* Test polymorphism
*
* @version 1.0
* @copyright
*/
Class Testpoly {
Public Function Drawnow (Shape $shape) {
$shape->draw ();
}
}
$test = new Testpoly ();
$test->drawnow (New Triangle ());
$test->drawnow (New Rectangle ());
/* Vim:set expandtab tabstop=4 shiftwidth=4: */
What is dynamic binding?
Haohappy translation of the PHP5 Object Pattern is described in section Nineth:
In addition to restricting access, the access mode also determines which method will be called by the quilt class or which property will be accessed by the quilt class. The Association of a function call to the function itself, and the relationship between member access and the memory address of a variable, is called a binding.
This article is from the "Baboon Baboon" blog, please be sure to keep this source http://9521583.blog.51cto.com/9511583/1627949
PHP5 polymorphism and dynamic binding method