Application of interface in PHP class--implements

Source: Internet
Author: User
Tags php class
The application key of the interface in PHP class is interface, implements, interface is a kind of special abstract class of member property all abstract or constant, implements is mainly to the class name, the method that the class owns, and the parameters that are passed and the specification are used, a bit like Abstract class.

Application of interfaces in class

1. Keywords: interface

2. Keywords: implements

1. Introduction and creation of the interface

Interface: A special abstract class in which member properties are all abstract or constant.

Rules:

1. All of the classes are abstract methods.

2. Abstract method Money is not added to abstract.

3. The interface abstract method property is public.

4. The member property must be a constant.

The format code is as follows:

Interface Demo {Const NAME = "Constant object property"; function myfun1 ();//abstract method function myfun2 ();//abstract method without specific write logic}

One, the definition and invocation of the interface

<?phpinterface cinema{const film = ' Pirates of the Caribbean ';p ublic function Show (); Class Order implements Cinema{public function Show ()    {echo "Cinema interface Open <br>";    }} $face = new Order (); Echo $face->show ();       Echo cinema::film;

Note: The above example should be noted that the method name of the interface is show, inheriting the interface of the class must have show this method, otherwise will be error. That is, the method of the interface is false, the real function is the method in the inherited class, where the interface and PHP abstract class is not a bit like?
Second, the parameter constraint is relatively strict

<?phpinterface cinema{public function Show (Order $show, $num);} Show Normal class Order implements Cinema{public $number = ' 0011 platoon ';p ublic function Show (Order $show, $num)    {echo $show Number. $num;    }} $face = New Order (), $face->show (New Order, $num = ' 3 people ');//Output 0011 rows 3 people

Note: The above example inherits from the interface class, when invoking the method of the interface, the parameters to be passed and the parameter names in the interface must be one to. Otherwise you will get an error.

Third, inheritance between interfaces and calling interface pass parameters

<?phpinterface cinema{public function Show (); Interface Cinema1 extends Cinema{public function show1 (Order1 $object, $num);} Class Order implements Cinema1{public function Show ()    {echo "readiness <br>";    } Public Function Show1 (Order1 $object, $num)    {//var_dump ($object); Echo $object->number. " $num <br> ";    }} Class Order1{public $number = "0012 rows"; function fun () {echo ' ================= ';    }} $show = new Order1; $show->fun ();          $test = new Order (); $test->show ();           $test->show1 ($show, $num = ' 6 people ');//output =============== ready 0012 rows 6 people

Note: The above example shows that the interface Cinemal1 inherits the interface Cinemal, and the class order inherits the interface Cinemal1. I don't know if you found out. Class order includes two methods, one is show, one show1, and one can not be less, if less one, reported fatal error. Order1 in the Show1 (Order1 $object, $num) must have the same name as Class Order1for the root-inheriting classes. If it is not the same, fatal error will also be reported. What if an interface is inherited by more than one class and the class name is different? It's going to be self.

Four, one interface multiple inheritance


<?phpinterface Demo {    const name = "movie name";    function fun1 ();    function fun2 ();} Interface Demo2 {    function fun3 ();    function Fun4 ();} Interface Demo3 {    const TEST = "Here is the test";    function Fun5 ();} Class Mydemo implements demo, Demo2 {    function fun1 () {        echo "Hello";    }    function fun2 () {        echo "----------";    }    function Fun3 () {        echo "I am also good <br/>";    }    function Fun4 () {        echo "Everybody good <br/>";}    } Class Yourdemo extends Mydemo implements DEMO3 {    function fun5 () {        echo "inherits class after reference interface";    }} $p = new Yourdemo; $p->fun1 (); $p->fun2 (); $p->fun3 (); $p->fun4 (); $p->fun5 ();

Above output

Hello----------I'm good, everybody's good. Inheriting class after reference interface

In the example above, we can see that the interfaces are defined using the keyword interface, and using the keyword implements to implement the methods in the interface, for example:

<?php//defines interface interface user{function getdiscount (); function Getusertype ();} Class Vipuser implements user{//VIP user interface realizes private $discount = 0.8;    VIP User discount Factor function Getdiscount () {return $this->discount;    } function Getusertype () {return "VIP user";    }}  Class Goods{var $price = 88;var $VC, function run (user $VC) {     //defines the user interface type parameter, and does not know what users $THIS->VC = $VC; $discount = $this->vc->getdiscount (); $usertype = $this->vc->getusertype (); Echo $usertype. " Commodity price: ". $this->price* $discount;    }} $display = new Goods (); $display->run (new Vipuser); VIP Users Product Price: 70.4

This example demonstrates a simple application of a PHP interface. In this example, the user interface implements a discount for users, and a specific discount factor is implemented within the Vipuser class. Finally, the product class Goods according to the user interface to achieve different customer quotes.

Finally, we summarize the following:

The difference between an abstract class and an interface

An interface is a special abstract class and can also be seen as a specification of a model. Interfaces are roughly different from abstract classes:

1. A subclass if you implements an interface, you must implement all the methods in the interface (whether or not it is required), or if you are inheriting an abstract class, you only need to implement the required method.

2. If the method name defined in one of the interfaces changes, then all subclasses implementing this interface need to update the method name synchronously, whereas in an abstract class if the method name changes, the corresponding method names of its subclasses are not affected, but only become a new method (relatively old method implementation).

3. Abstract classes can only be inherited, and when a subclass needs to implement functionality that needs to inherit from more than one parent class, the interface must be used.

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.