主要对类名,类所拥有的方法,以及所传参数起约束和规范做用,感觉跟php abstract 抽象类又有点像。

一,接口的定义和调用

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

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

二,对参数约束比较严

  1. <?php
  2. interface face1
  3. {
  4. public function show(show $show);
  5. }
  6. // 显示正常
  7. class test implements face1
  8. {
  9. public function show(show $show)
  10. {
  11. echo "asdfasdf";
  12. }
  13. }
  14. // 报fatal错误
  15. class test2 implements face1
  16. {
  17. public function show(aaa $aaa)
  18. {
  19. }
  20. }
  21. ?>

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

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

  1. <?php
  2. interface face1
  3. {
  4. public function show();
  5. }
  6. interface face2 extends face1
  7. {
  8. public function show1(test1 $test,$num);
  9. }
  10. class test implements face2
  11. {
  12. public function show()
  13. {
  14. echo "ok<br>";
  15. }
  16. public function show1(test1 $test,$num)
  17. {
  18. var_dump($test);
  19. echo $test1->aaaa."$num<br>";
  20. }
  21. }
  22. class test1
  23. {
  24. public $aaaa="this is a test";
  25. function fun(){
  26. echo ' ===============<br>';
  27. }
  28. }
  29. $show = new test1;
  30. $show->fun();            //显示===============
  31. test::show();             //显示ok
  32. test::show1($show,6);     //object(test1)#1 (1) { ["aaaa"]=>  string(14) "this is a test" } 6
  33. ?>

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

四,一个接口多个继承

  1. <?php
  2. interface Comparable {
  3. function compare(self $compare);
  4. }
  5. class String implements Comparable {
  6. private $string;
  7. function __construct($string) {
  8. $this->string = $string;
  9. }
  10. function compare(self $compare) {
  11. if($this->string == $compare->string){
  12. return $this->string."==".$compare->string."<br>";
  13. }else{
  14. return $this->string."!=".$compare->string."<br>";
  15. }
  16. }
  17. }
  18. class Integer implements Comparable {
  19. private $integer;
  20. function __construct($int) {
  21. $this->integer = $int;
  22. }
  23. function compare(self $compare) {
  24. if($this->integer == $compare->integer){
  25. return $this->integer."==".$compare->integer."<br>";
  26. }else{
  27. return $this->integer."!=".$compare->integer."<br>";
  28. }
  29. }
  30. }
  31. $first_int = new Integer(3);
  32. $second_int = new Integer(4);
  33. $first_string = new String("foo");
  34. $second_string = new String("bar");
  35. echo $first_int->compare($second_int);              // 3!=4
  36. echo $first_int->compare($first_int);               // 3==3
  37. echo $first_string->compare($second_string);        // foo!=bar
  38. echo $first_string->compare($second_int);           // 严重错误
  39. ?>

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

最新文章

  1. printf对齐
  2. 好神奇的代码,可以让匿名用户对特定SharePoint 列表拥用添加列表项的权限哦
  3. [转]C#反射-Assembly.Load、LoadFrom与LoadFile进阶
  4. 极简Photoshop 教程
  5. hibernate关联映射学习
  6. Object学习笔记
  7. 创建plist文件
  8. 四.CSS声明
  9. Document.getElementById 与 $(&#39;#id&#39;)的区别
  10. centos 6.5 hadoop 2.3 初配置
  11. Perl 之 use(), require(), do(), %INC and @INC
  12. 【MyBatis源码分析】insert方法、update方法、delete方法处理流程(下篇)
  13. Android存储系统的架构与设计
  14. ISOMAP和MDS降维
  15. skynet记录2:模块简介
  16. 实现 AD 采样,使用 LCD1602 显示 AD 数值
  17. CMUSphinx Learn - Basic concepts of speech
  18. 伪共享(False Sharing)
  19. WCF 学习文摘
  20. java代码=====实现修改while()

热门文章

  1. 记RedisDesktopManager的一次崩溃
  2. 点击modal确定键后删除tr
  3. MongoDB--使用修改器修改文档
  4. UML 2中结构图的介绍
  5. iOS 使用CLGeocoder获取地理位置
  6. [Unity] How to stop camera rendering?
  7. [React Native] Passing data when changing routes
  8. 没有找到MSVCR100.dll解决方法
  9. careercup-排序和查找 11.2
  10. oracle学习----DDL锁理解