In PHP Tutorial 5, the type of variables is undefined. A variable can point to any type of numerical value, string, object, resource, etc. We cannot say that polymorphism in php5 is a variable.
We can only say that in php5, polymorphism is applied in the type hint position of method parameters.
Any subclass object of a class can satisfy the type requirement with the current type as a type hint. All classes that implement this interface can meet the method parameter requirements with the interface type as a type hint. Simply put, a class has the identity of its parent class and the implemented interface
Polymorphism is achieved by implementing the interface
In the following example, the static method of the useradmin class requires a user type parameter.
In subsequent uses, 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");//What is passed in here is the instance of normaluser.
echo $normaluser->getname();
?>
PHP interface classes: interface
In fact, their function is very simple. When many people develop a project together, they may all call some classes written by others. Then you will ask, how do I know something of his? How to name the implementation method of each function? At this time, the PHP interface class comes into play. When we define an interface class, the methods in it must be implemented by the following subclasses, such as:
The code is as follows:
interface shop
{
public function buy($gid);
public function sell($gid);
public function view($gid);
}
I declare a shop interface class and define three methods: buy, sell, and view. Then all subclasses that inherit this class must implement this None of the three methods will work. If the subclass does not implement these, it will not work. In fact, the interface class is, to put it bluntly, the template of a class and the regulations of a class. If you belong to this category, you must follow my regulations. No matter how you do it, I don’t care how you do it. That’s up to you. Things like:
The code is as follows:
class baseshop implements shop
{
public function buy($gid)
{
echo('You buy The product with the id: '.$gid.');
}
public function sell($gid)
{
echo('You sold the product with the id:'.$gid. 'Product');
}
public function view($gid)
{
echo('You viewed the product with id: '.$gid.');
}
}
The following is a shortened method
interface myusbkou
{
function type();// Type
function action();//The operation performed
}
class zip implements myusbkou
{ //Inherited interface
function type()
{
echo "usb 2.0 interface";
}
function action()
{
echo "--->requires usb 2.0 driver";
}
}
class mp3 implements myusbkou
{
function type()
{
echo "mp3 1.0 interface";
}
function action()
{
echo "--- >Requires mp3 1.0 driver
";
}
}
class mypc
{
function usbthing($thing)
{
$thing- >type();
$thing->action();
}
}
$p=new mypc(); >$zip=new zip();
$p->usbthing($mp3);
$p->usbthing($zip);
?>