In PHP Tutorial 5, the type of the variable is indeterminate, and a variable can point to any type of numeric value, string, object, resource, and so on. We cannot say that the polymorphic variables in PHP5 are variable.
We can only say that in php5, polymorphism is applied to the type hint position of the method parameter.
Any subclass object of a class can satisfy the type requirement of the current type as a type hint. All classes that implement this interface can satisfy the method parameter requirements with the interface type as a type hint. Simply put, a class has its parent class, and the identity of the implemented interface
Achieve polymorphism by implementing interfaces
In the following example, the static method of the Useradmin class requires a parameter of type user.
In later use, an instance of the class Normaluser 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 {//Implement interface classes.
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");//Here is an instance of Normaluser.
echo $normaluser->getname ();
?>
PHP Interface Class: interface
In fact, their role is very simple, when a lot of people to develop a project, it may be to call someone else to write some of the classes, then you will ask, how do I know how the implementation of one of his functions is named, this time the PHP interface class will play a role, when we define an interface class, The way inside it is that the following subclass must be implemented, 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, defines three methods: Buy, Sell (Sell), see (view), then inherit all subclasses of this class must implement these 3 methods less than one, if the subclass does not implement these words, it will not run. In fact, the interface class is plainly, is a class template, a class of provisions, if you belong to this category, you have to follow my rules, less one can not, but specific how you do, I don't care, it is your business, such as:
The code is as follows:
class Baseshop implements Shop
{
Public function Buy ($gid)
{
Echo (' You have purchased the ID: '. $gid. ' goods ');
}
Public function Sell ($gid)
{
Echo (' You have sold the ID: '. $gid. ' goods ');
}
Public Function view ($gid)
{
Echo (' You have viewed the item ID: '. $gid. ');
}
}
Here's a way to indent
Interface Myusbkou
{
function type ();//Type
function action ();//Action performed
}
Class Zip implements Myusbkou
{//Inherit 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 drive
";
}
}
Class MyPC
{
function usbthing ($thing)
{
$thing->type ();
$thing->action ();
}
}
$p =new MyPC ();
$MP 3=new mp3 ();
$zip =new zip ();
$p->usbthing ($mp 3);
$p->usbthing ($zip);
?>
http://www.bkjia.com/PHPjc/444782.html www.bkjia.com true http://www.bkjia.com/PHPjc/444782.html techarticle in PHP Tutorial 5, the type of the variable is indeterminate, and a variable can point to any type of numeric value, string, object, resource, and so on. We cannot say that the polymorphic variables in PHP5 are variable. We just ...