一,接口的定義和調(diào)用 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 run<br>"; 13 } 14 } 15 16 $face = new test(); 17 echo $face->show(); //inerface is run 18 echo face1::param; //test 19 ?> 說(shuō)明:上面的例子要注意一點(diǎn),接口的方法名是show,繼承接口的類中必須有show這個(gè)方法,要不然就會(huì)報(bào)錯(cuò)。也就是說(shuō)接口的方法是假的,真正起作用的是在繼承的類中的方法,就是因?yàn)檫@一點(diǎn),所以我覺(jué)得,接口根php的抽象類有點(diǎn)像。 二,對(duì)參數(shù)約束比較嚴(yán) <?php interface face1 { public function show(show $show); } // 顯示正常 class test implements face1 { public function show(show $show) { echo "asdfasdf"; } } // 報(bào)fatal錯(cuò)誤 class test2 implements face1 { public function show(aaa $aaa) { } } ?> 說(shuō)明:上面的這個(gè)例子報(bào)fatal錯(cuò)誤的,為什么會(huì)報(bào)fatal錯(cuò)誤呢?原因就在所傳參數(shù)是aaa $aaa,而不是show $show。繼承接口類中,調(diào)用接口的方法時(shí),所傳參數(shù)要和接口中的參數(shù)名要一至。不然就會(huì)報(bào)錯(cuò)。 三,接口間的繼承和調(diào)用接口傳遞參數(shù) <?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 $test1->aaaa."$num<br>"; } } class test1 { public $aaaa="this is a test"; function fun(){ echo ' ===============<br>'; } } $show = new test1; $show->fun(); //顯示=============== test::show(); //顯示ok test::show1($show,6); //object(test1)#1 (1) { ["aaaa"]=> string(14) "this is a test" } 6 ?> 說(shuō)明:上面的例子可以看到,接口face2繼承了接口face1,類test繼承了接口face2。不知道你發(fā)現(xiàn)沒(méi)有,class類test當(dāng)中包括有二個(gè)方法,一個(gè)是show,一個(gè)show1,并且一個(gè)也不能少,如果少一個(gè),報(bào)fatal錯(cuò)誤。show1(test1 $test,$num)中的test1必須根繼承類的名子要一樣classtest1。如果不一樣,也會(huì)報(bào)fatal錯(cuò)誤。那如果一個(gè)接口被多個(gè)類繼承,并且類名又不一樣,怎么辦呢?那就要用self了,下面會(huì)提到 四,一個(gè)接口多個(gè)繼承 <?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); // 嚴(yán)重錯(cuò)誤 ?> 說(shuō)明:從上面的例子中可以看出,一個(gè)接口可以被多個(gè)類繼承,并且類名不一樣。同一個(gè)類之間可以相互調(diào)用,不同類之間不能調(diào)用。echo $first_string->compare($second_int);報(bào)fatal錯(cuò)誤的。 |
|
來(lái)自: 醉人說(shuō)夢(mèng) > 《PHP》