php面向对象编程中,接口是通过interface关键字来定义的没有方法体的方法,可以包含参数,作用是被类调用来实现面向对象的多态的.
接口是让类用来继承的,接口中不能包含任何代码,所以所有接口只能通过类的集成来实现具体的方法,且接口的方法类必须全部实现,否则,会出现语法错误
下面来看具体的实现
这是具体的实现代码
<?php
/**
* Created by PhpStorm.
* User: WangLei
* Date: 2018/11/26
* Time: 14:54
*/
interface Test{
public function test1();
public function test2();
}
interface myTest{
public function test3();
public function test4();
}
class C implements Test,myTest {
public function test1()
{
// TODO: Implement test1() method.
}
public function test2()
{
// TODO: Implement test2() method.
}
public function test3()
{
// TODO: Implement test3() method.
}
public function test4()
{
// TODO: Implement test4() method.
}
}
$test=123;
$b=new B($test);
echo $b->getClass().$test;
说明 定义 Test和myTest两个接口,在接口中定义无函数体的空方法
然后 C 这个class 通过implements 关键字继承Test,myTest两个接口(多个接口之间使用,分开).然后在类里实现具有相同访问属性的方法,就能实现接口的方法了,通过不同的类的,实现不同的方法功能
composer的包的组件有好多就是通过接口实现对php基本类的使用,来实现不同的组件和功能,方便我们的使用
初次写博客,不足之处,欢迎指正