Mainly for the class name, the methods owned by the class, as well as the parameters of the arguments to do with constraints and specifications to do, it feels like PHP abstract class and a bit similar.
One, the definition and invocation of the interface
- <?php
- Interface Face1
- {
- const PARAM = ' test ';
- public function Show ();
- }
- Class Test implements Face1
- {
- public function Show ()
- {
- echo "interface is run<br>";
- }
- }
- $face = new Test ();
- echo $face->show (); //inerface is run
- echo Face1::p Aram; //test
- ?>
Note: The above example should be noted that the method name of the interface is show, inherit the interface class must have show this method, otherwise will be error. That is, the method of the interface is false, the real function is in the inheriting class method, because of this, so I think, interface root PHP abstract class A bit like.
Second, the parameter constraint is relatively strict
- <?php
- Interface Face1
- {
- public function Show (show $show);
- }
- Show Normal
- Class Test implements Face1
- {
- public function Show (show $show)
- {
- echo "ASDFASDF";
- }
- }
- Report fatal error
- Class Test2 implements Face1
- {
- public function Show (AAA $aaa)
- {
- }
- }
- ?>
Explanation: The above example reported fatal error, why will the report fatal error? The reason is that the argument is the AAA $aaa, not the show $show. In the Inherited interface class, when invoking the method of an 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
- <?php
- Interface Face1
- {
- public function Show ();
- }
- Interface Face2 extends Face1
- {
- Public function Show1 (test1 $test,$num);
- }
- Class Test implements Face2
- {
- public function Show ()
- {
- echo "ok<br>";
- }
- Public function Show1 (test1 $test,$num)
- {
- Var_dump ($test);
- echo $test 1->aaaa." $num <br> ";
- }
- }
- Class Test1
- {
- public $aaaa ="This is a test";
- function Fun () {
- echo ' ===============<br> ';
- }
- }
- $show = new Test1;
- $show->fun (); //Display ===============
- Test::show (); //show OK
- Test::show1 ($show, 6); //object (test1) #1 (1) {["AAAA"]=> string () "This is a test"} 6
- ?>
Note: The above example can be seen, interface Face2 inherits the interface Face1, class test inherits the interface Face2. I don't know if you found out. Class Test includes two methods, one is show, one show1, and one can not be less, if less one, reported fatal error. The test1 in Show1 (test1 $test, $num) must have the same class test1as the name of 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
- <?php
- Interface Comparable {
- function compare (self $compare);
- }
- Class String implements comparable {
- private $string;
- function __construct ($string) {
- $this->string = $string;
- }
- function compare (self $compare) {
- if ($this->string = = $compare->string) {
- return $this->string." ==". $compare->string. "<br>";
- }else{
- return $this->string."! =". $compare->string. "<br>";
- }
- }
- }
- 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. "<br>";
- }else{
- return $this->integer."! =". $compare->integer. "<br>";
- }
- }
- }
- $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!=bar
- echo $first _string->compare ($second _int); //Critical error
- ?>
Note: As can be seen from the above example, an interface can be inherited by multiple classes, and the class name is different. The same class can be called from one another and cannot be called between different classes. echo $first _string->compare ($second _int); fatal wrong.
PHP interface implements use