在PHP中implement什么意思,php 接口(implement,implements)的学习和使用

刚开始学习php的时候,看底层东西就看到过implements这类的东西,当时要学的东西好多,也没有特别多的时间来学习接口的东西。最近看代码的时候,无意中又看到了这个东西,花了点时间研究了一下。

总体感觉,他主要对类名,类所拥有的方法,以及所传参数起约束和规范做用,感觉根php abstract 抽象类又有点像。如果做大项目的话,估计可以启点作用。不过如果是大项目的话,开始写代码前,肯定会定一些写代码的规则,类的名子怎么起,方法名怎么起,怎么继承,都会有所说明,个人觉得这个implement没有多大用处。下面是学习的时候,弄的一些例子。

一,接口的定义和调用

interface face1

{

const param = 'test';

public function show();

}

class test implements face1

{

public function show()

{

echo "interface is run
";

}

}

$face = new test();

echo $face->show(); //inerface is run

echo face1::param; //test

?>

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

二,对参数约束比较严

interface face1

{

public function show(show $show);

}

// 显示正常

class test implements face1

{

public function show(show $show)

{

echo "asdfasdf";

}

}

// 报fatal错误

class test2 implements face1

{

public function show(aaa $aaa)

{

}

}

?>

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

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

interface face1

{

public function show();

}

interface face2 extends face1

{

public function show1(test1 $test,$num);

}

class test implements face2

{

public function show()

{

echo "ok
";

}

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();            //显示===============

test::show();             //显示ok

test::show1($show,6);     //object(test1)#1 (1) { ["aaaa"]=>  string(14) "this is a test" } 6

?>

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

四,一个接口多个继承

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."
";

}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!=bar

echo $first_string->compare($second_int);           // 严重错误

?>

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值