Applications with polymorphism in the php tutorial class
Instanceof: used to determine whether a given object comes from a specified object class.
<? Php
Class {}
Class B {}
$ Thing = new ();
// Return true
If ($ thing instanceof ){
Echo 'a ';
}
// Return false
If ($ thing instanceof B ){
Echo 'B ';
}
?>
Running result:
A
<? Php
Header ("Content-Type: text/html; charset = UTF-8 ");
Interface MyUsb {
Function type ();
Function alert ();
}
Class Zip implements MyUsb {
Function type (){
Echo & quot; 2.0 & quot ";
}
Function alert (){
Echo "Checking USB flash drive ";
}
}
Class Mp3 implements MyUsb {
Function type (){
Echo & quot; 1.0 & quot ";
}
Function alert (){
Echo "Checking Mp3 driver ";
}
}
Class Mypc {
Function PcUsb ($ what)
{
$ What-> type ();
$ What-> alert ();
}
}
$ Pc = new Mypc ();
$ Zip = new Zip ();
$ Mp3 = new Mp3 ();
$ Pc-> PcUsb ($ zip); // when a USB flash drive is inserted
Echo "<br/> ";
$ Pc-> PcUsb ($ mp3); // when MP3 is inserted
?>
Running result:
2.0 checking USB flash drive
1.0 checking Mp3 driver
Object references are the same, regardless of parent class references or subclass references. Now let's look at an example. To use polymorphism, there must be a relationship between the parent class object and the subclass object. Create a shape interface or abstract class as the parent class. There are two abstract methods, one for perimeter and the other for area; the sub-classes of this interface have different shapes, each of which has a length and area, and because the parent class is an interface, therefore, the child classes must implement the abstract methods of the two weeks and areas of the parent class, so that the child classes of different shapes comply with the parent class interface specifications, you must have a method to determine the length of Week and the area.
The Code is as follows:
<?
// Defines a shape interface, which has two abstract methods for sub-classes to implement
Interface Shape {
Function area ();
Function perimeter ();
}
// Defines a rectangular subclass to implement the circumference and area of the Shape interface.
Class Rect implements Shape {
Private $ width;
Private $ height;
Function _ construct ($ width, $ height ){
$ This-> width = $ width;
$ This-> height = $ height;
}
Function area (){
Return "The area of the rectangle is:". ($ this-> width * $ this-> height );
}
Function perimeter (){
Return "the perimeter of the rectangle is:". (2 * ($ this-> width + $ this-> height ));
}
}
// Defines a circular subclass to implement the circumference and area of the Shape interface.
Class Circular implements Shape {
Private $ radius;
Function _ construct ($ radius ){
$ This-> radius = $ radius;
}
Function area (){
Return "the circular area is:". (3.14 * $ this-> radius );
}
Function perimeter (){
Return "the circumference of the circle is:". (2*3.14 * $ this-> radius );
}
}
// Assign the subclass rectangle object to a reference of the shape
$ Shape = new Rect (5, 10 );
Echo $ shape-> area (). "<br> ";
Echo $ shape-> perimeter (). "<br> ";
// Assign the circular object of the subclass to a reference of the shape.
$ Shape = new Circular (10 );
Echo $ shape-> area (). "<br> ";
Echo $ shape-> perimeter (). "<br> ";
?>
Result of the preceding example:
Execution result
The area of the rectangle is: 50.
The circumference of the rectangle is: 30.
Circular area: 314
The circle circumference is: 62.8
In the above example, we can see that the rectangle object and the circle object are assigned to the variable $ shape, and the $ shape reference is called.
The area and perimeter of the method, there are different results, this is a kind of polymorphism application, in fact, in our PHP such weak
In the object-oriented language of the class, the characteristics of polymorphism are not particularly obvious. In fact, it is the variable of the object type.
Application.