Php polymorphism and interface learning implementation and instance code

Source: Internet
Author: User

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 );
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.