Interface in PHP can only play a role in the definition of constraint classes, although not as intuitive as C#/java, but based on OOP encapsulation requirements, the use of interfaces can improve the scalability of the program, such as the implementation of proxy design mode.
Copy Code code as follows:
<?php
Human interface
Interface Ihuman
{
function GetName ();
}
Male human, realize human interface
Class Manclass implements Ihuman
{
Get Name method
Public Function GetName ()
{
Return "I ' m man." <br> ";
}
}
Woman human, realize human interface
Class Womanclass implements Ihuman
{
Get Name method
Public Function GetName ()
{
Return "I ' m Woman." <br> ";
}
}
class factory, produce different instance objects as needed to return
Class Manfactory
{
Get instance object from parameters
Public Function Getihuman ($IHuman = "mans")
{
if ($IHuman = = "Woman")
{
return new Womanclass ();
}
else if ($IHuman = = "Man")
{
return new Manclass ();
}
Else
{
return null;
}
}
Get woman class directly
Public Function Getwoman ()
{
return new Womanclass ();
return new Manclass ();
}
Get the man type directly
Public Function Getman ()
{
return new Manclass ();
}
}
$ManFactory =new manfactory ();
$ManClass = $ManFactory->getihuman ();
echo $ManClass->getname ();
$IHuman = $ManFactory->getihuman ("Woman");
echo $IHuman->getname ();
$Woman = $ManFactory->getwoman ();
echo $Woman->getname ();
$Man = $ManFactory->getman ();
echo $Man->getname ();
?>
Run Result:
I ' m man.
I ' m Woman.
I ' m Woman.
I ' m man.