首页  >  文章  >  php教程  >  php 接口(implement,implements)的学习和使用

php 接口(implement,implements)的学习和使用

WBOY
WBOY原创
2016-06-06 19:49:321357浏览

一,接口的定义和调用 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();

一,接口的定义和调用

<span> 1</span> <span>php
</span><span> 2</span> <span>interface</span><span> face1
</span><span> 3</span> <span>{
</span><span> 4</span>  <span>const</span> param = 'test'<span>;
</span><span> 5</span>  <span>public</span> <span>function</span><span> show();
</span><span> 6</span> <span>}
</span><span> 7</span> 
<span> 8</span> <span>class</span> test <span>implements</span><span> face1
</span><span> 9</span> <span>{
</span><span>10</span>  <span>public</span> <span>function</span><span> show()
</span><span>11</span> <span> {
</span><span>12</span>  <span>echo</span> "interface is run<br>"<span>;
</span><span>13</span> <span> }
</span><span>14</span> <span>}
</span><span>15</span> 
<span>16</span> <span>$face</span> = <span>new</span><span> test();
</span><span>17</span> <span>echo</span> <span>$face</span>->show();         <span>//</span><span>inerface is run</span>
<span>18</span> <span>echo</span> face1::param;           <span>//</span><span>test</span>
<span>19</span> ?>

说明:上面的例子要注意一点,接口的方法名是show,继承接口的类中必须有show这个方法,要不然就会报错。也就是说接口的方法是假的,真正起作用的是在继承的类中的方法,就是因为这一点,所以我觉得,接口根php的抽象类有点像。

二,对参数约束比较严

<span>php
</span><span>interface</span><span> face1
{
 </span><span>public</span> <span>function</span> show(show <span>$show</span><span>);
}

</span><span>//</span><span> 显示正常</span>
<span>class</span> test <span>implements</span><span> face1
{
 </span><span>public</span> <span>function</span> show(show <span>$show</span><span>)
 {
 </span><span>echo</span> "asdfasdf"<span>;
 }
}

</span><span>//</span><span> 报fatal错误</span>
<span>class</span> test2 <span>implements</span><span> face1
{
 </span><span>public</span> <span>function</span> show(aaa <span>$aaa</span><span>)
 {
 }
}
</span>?>

说明:上面的这个例子报fatal错误的,为什么会报fatal错误呢?原因就在所传参数是aaa $aaa,而不是show $show。继承接口类中,调用接口的方法时,所传参数要和接口中的参数名要一至。不然就会报错。

三,接口间的继承和调用接口传递参数

<span>php
</span><span>interface</span><span> face1
{
 </span><span>public</span> <span>function</span><span> show();
}

</span><span>interface</span> face2 <span>extends</span><span> face1
{
 </span><span>public</span> <span>function</span> show1(test1 <span>$test</span>,<span>$num</span><span>);
}

</span><span>class</span> test <span>implements</span><span> face2
{
 </span><span>public</span> <span>function</span><span> show()
 {
 </span><span>echo</span> "ok<br>"<span>;
 }

 </span><span>public</span> <span>function</span> show1(test1 <span>$test</span>,<span>$num</span><span>)
 {
 </span><span>var_dump</span>(<span>$test</span><span>);
 </span><span>echo</span> <span>$test1</span>->aaaa."<span>$num</span><br>"<span>;
 }
}

</span><span>class</span><span> test1
{
 </span><span>public</span> <span>$aaaa</span>="this is a test"<span>;
 </span><span>function</span><span> fun(){
 </span><span>echo</span> ' ===============<br>'<span>;
 }
}

</span><span>$show</span> = <span>new</span><span> test1;
</span><span>$show</span>->fun();            <span>//</span><span>显示===============</span>
test::show();             <span>//</span><span>显示ok</span>
test::show1(<span>$show</span>,6);     <span>//</span><span>object(test1)#1 (1) { ["aaaa"]=>  string(14) "this is a test" } 6</span>
?>

说明:上面的例子可以看到,接口face2继承了接口face1,类test继承了接口face2。不知道你发现没有,class类test当中包括有二个方法,一个是show,一个show1,并且一个也不能少,如果少一个,报fatal错误。show1(test1 $test,$num)中的test1必须根继承类的名子要一样classtest1。如果不一样,也会报fatal错误。那如果一个接口被多个类继承,并且类名又不一样,怎么办呢?那就要用self了,下面会提到

四,一个接口多个继承

<span>php
</span><span>interface</span><span> Comparable {
 </span><span>function</span> compare(self <span>$compare</span><span>);
}

</span><span>class</span> <span>String</span> <span>implements</span><span> Comparable {
 </span><span>private</span> <span>$string</span><span>;
 </span><span>function</span> __construct(<span>$string</span><span>) {
 </span><span>$this</span>-><span>string</span> = <span>$string</span><span>;
 }

 </span><span>function</span> compare(self <span>$compare</span><span>) {
 </span><span>if</span>(<span>$this</span>-><span>string</span> == <span>$compare</span>-><span>string</span><span>){
 </span><span>return</span> <span>$this</span>-><span>string</span>."==".<span>$compare</span>-><span>string</span>."<br>"<span>;
 }</span><span>else</span><span>{
 </span><span>return</span> <span>$this</span>-><span>string</span>."!=".<span>$compare</span>-><span>string</span>."<br>"<span>;
 }
 }
}

</span><span>class</span> <span>Integer</span> <span>implements</span><span> Comparable {
 </span><span>private</span> <span>$integer</span><span>;
 </span><span>function</span> __construct(<span>$int</span><span>) {
 </span><span>$this</span>-><span>integer</span> = <span>$int</span><span>;
 }

 </span><span>function</span> compare(self <span>$compare</span><span>) {
 </span><span>if</span>(<span>$this</span>-><span>integer</span> == <span>$compare</span>-><span>integer</span><span>){
 </span><span>return</span> <span>$this</span>-><span>integer</span>."==".<span>$compare</span>-><span>integer</span>."<br>"<span>;
 }</span><span>else</span><span>{
 </span><span>return</span> <span>$this</span>-><span>integer</span>."!=".<span>$compare</span>-><span>integer</span>."<br>"<span>;
 }
 }
}

</span><span>$first_int</span> = <span>new</span> <span>Integer</span>(3<span>);
</span><span>$second_int</span> = <span>new</span> <span>Integer</span>(4<span>);
</span><span>$first_string</span> = <span>new</span> <span>String</span>("foo"<span>);
</span><span>$second_string</span> = <span>new</span> <span>String</span>("bar"<span>);

</span><span>echo</span> <span>$first_int</span>->compare(<span>$second_int</span>);              <span>//</span><span> 3!=4</span>
<span>echo</span> <span>$first_int</span>->compare(<span>$first_int</span>);               <span>//</span><span> 3==3</span>
<span>echo</span> <span>$first_string</span>->compare(<span>$second_string</span>);        <span>//</span><span> foo!=bar</span>
<span>echo</span> <span>$first_string</span>->compare(<span>$second_int</span>);           <span>//</span><span> 严重错误</span>
?>

说明:从上面的例子中可以看出,一个接口可以被多个类继承,并且类名不一样。同一个类之间可以相互调用,不同类之间不能调用。echo $first_string->compare($second_int);报fatal错误的。

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn