In php Tutorial 5, the variable type is uncertain. A variable can point to any type of value, string, object, resource, etc. We cannot say that the polymorphism in php5 is a variable.
We can only say that in php5, polymorphism is applied to the type prompt position of method parameters.
Any subclass object of a class can meet the type requirements with the current type as the type prompt. All classes that implement this interface can meet the method parameter requirements that use the interface type as the type prompt. Simply put, a class has its parent class and its identity as implemented interfaces.
Implement polymorphism through the implementation interface
In the following example, the static method of the useradmin class requires a user-type parameter.
In subsequent use, an instance of the normaluser class that implements the user interface is passed. The code runs successfully.
<?
Interface user {// user interface
Public function getname ();
Public function setname ($ _ name );
}
Class normaluser implements user {// class that implements the interface.
Private $ name;
Public function getname (){
Return $ this-> name;
}
Public function setname ($ _ name ){
$ This-> name = $ _ name;
}
}
Class useradmin {// operation.
Public static function changeusername (user $ _ user, $ _ username ){
$ _ User-> setname ($ _ username );
}
}
$ Normaluser = new normaluser ();
Useradmin: changeusername ($ normaluser, "tom"); // The normaluser instance is input here.
Echo $ normaluser-> getname ();
?>
Php interface class: interface
In fact, their role is very simple. When many people develop a project together, they may call some classes written by others, and you will ask, how do I know how the implementation method of a function is named? At this time, the php interface class plays a role. When we define an interface class, the method in it must be implemented by the following sub-classes, for example:
The code is as follows:
Interface shop
{
Public function buy ($ gid );
Public function compute ($ gid );
Public function view ($ gid );
}
I declare a shop interface class and define three methods: buy, sell, view ), therefore, none of the three methods must be implemented for all subclasses that inherit this class. If the subclass does not implement these methods, it will not be able to run. Actually, an interface class is a class template and a class rule. If you belong to this class, you must follow my rules, but how do you do it? I don't care. It's your business, for example:
The code is as follows:
Class baseshop implements shop
{
Public function buy ($ gid)
{
Echo ('item with id: '. $ gid.' You purchased ');
}
Public function compute ($ gid)
{
Echo ('item with id: '. $ gid.' You sold ');
}
Public function view ($ gid)
{
Echo ('The item with id: '. $ gid.' You viewed ');
}
}
The following method is used:
<? Php
Interface myusbkou
{
Function type (); // type
Function action (); // The operation executed
}
Class zip implements myusbkou
{// Inherited interface
Function type ()
{
Echo "2.0 usb interface ";
}
Function action ()
{
Echo "---> usb 2.0 driver required ";
}
}
Class mp3 implements myusbkou
{
Function type ()
{
Echo "mp3 1.0 interfaces ";
}
Function action ()
{
Echo "---> requires an mp3 1.0 driver <br/> ";
}
}
Class mypc
{
Function USB thing ($ thing)
{
$ Thing-> type ();
$ Thing-> action ();
}
}
$ P = new mypc ();
$ Mp3 = new mp3 ();
$ Zip = new zip ();
$ P-> USB thing ($ mp3 );
$ P-> USB thing ($ zip );
?>