Learning and using the php interface (implement, implements)

Source: Internet
Author: User
I. Interface Definition and call 1? Php2interface13 {4 constparamtest; 5 publicfunctionshow (); 6} 78classtestimplementsface19 {10 publicfunctionshow () 11 {12 echointerfaceisrunbr; 13} 14} 1516 $ facenewtest ();

I. Interface Definition and call 1? Php 2 interface face1 3 {4 const param = 'test'; 5 public function show (); 6} 7 8 class test implements face1 9 {10 public function show () 11 {12 echo "interface is runbr"; 13} 14} 15 16 $ face = new test ();

1. Interface Definition and call

 1 
 ";13  }14 }15 16 $face = new test();17 echo $face->show();         //inerface is run18 echo face1::param;           //test19 ?>

Note: In the preceding example,The method name of the interface is show, Inherited InterfaceClass must have showOtherwise, an error is reported. That is to say, the interface method is false. What really works is the method in the inherited class. This is because of this, so I think the abstract class of the interface root php is a bit like.

2. Strict parameter Constraints

 

Note: The above example reports a fatal error. Why does it report a fatal error? The reason is that the parameter isAaa $ aaaInsteadShow $ show. In the inherited interface class, when calling the interface method, the passed parameters must be the same as the parameter names in the interface. Otherwise, an error is reported.

3. inheritance between interfaces and parameters passed by calling Interfaces

 ";} Public function show1 (test1 $ test, $ num) {var_dump ($ test); echo $ test1-> aaaa." $ num
";}} Class test1 {public $ aaaa =" this is a test "; function fun () {echo '==============
';}}$ Show = new test1; $ show-> fun (); // display ====================== test :: show (); // display oktest: show1 ($ show, 6); // object (test1) #1 (1) {["aaaa"] => string (14) "this is a test"} 6?>

Note: The preceding example shows that interface face2 inherits interface face1 and class test inherits interface face2. I don't know if you have found that there are two methods in the class test, one is show, one is show1, and the other is not less. If there is one fewer, a fatal error is reported. Show1 (Test1In $ test, $ num)Test1The name of the root inheritance class must be the same as that of the class.Test1. If they are different, a fatal error is also reported. What if an interface is inherited by multiple classes with different class names? So we need to use self, which will be mentioned below

4. Multiple inheritance for one interface

 String = $ string;} function compare (self $ compare) {if ($ this-> string = $ compare-> string) {return $ this-> string. "= ". $ compare-> string."
";} Else {return $ this-> string ."! = ". $ Compare-> string ."
";}}} Class Integer implements Comparable {private $ integer; function _ construct ($ int) {$ this-> integer = $ int;} function compare (self $ compare) {if ($ this-> integer ==$ compare-> integer) {return $ this-> integer. "= ". $ compare-> integer."
";} Else {return $ this-> integer ."! = ". $ Compare-> integer ."
";}}$ First_int = new Integer (3); $ second_int = new Integer (4); $ first_string = new String (" foo "); $ second_string = new String ("bar"); echo $ first_int-> compare ($ second_int); // 3! = 4 echo $ first_int-> compare ($ first_int); // 3 = 3 echo $ first_string-> compare ($ second_string); // foo! = Barecho $ first_string-> compare ($ second_int); // serious error?>

Note: As shown in the preceding example, an interface can be inherited by multiple classes with different class names. The same class can be called each other, but different classes cannot. Echo $ first_string-> compare ($ second_int); the fatal error is returned.

Related Article

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.