一. 接口:
1.采用一个特定接口的所有代码都知道对于那个接口会调用什么方法。接口除了可以包含方法外还能包含常量。
2.接口为抽象而生。
4.补充:PHP5对面向对象的特性做了增强,其中有SPL(标准PHP库)。SPL中能实现很多接口,其中最主要的是Interator接口。
1.采用一个特定接口的所有代码都知道对于那个接口会调用什么方法。接口除了可以包含方法外还能包含常量。
2.接口为抽象而生。
<?php
header("Content-type: text/html; charset=utf-8");
interface mobile{
public function run();
}
class plain implements mobile{
public function run(){
echo "我是灰机我是灰机";
}
public function fly(){
echo "飞呀飞";
}
}
class car implements mobile{
public function run(){
echo "我是汽车我是汽车\r\n";
}
}
class machine{
function demo(mobile $a){
$a->fly();
}
}
$obj = new machine();
$obj->demo(new plain());
$obj->demo(new car());
3.PHP的接口可以淡化为设计文档:
4.补充:PHP5对面向对象的特性做了增强,其中有SPL(标准PHP库)。SPL中能实现很多接口,其中最主要的是Interator接口。
<?php
$dir = new DirectoryIterator(dirname(__FILE__));
foreach ($dir as $fileinfo){
if (!$fileinfo -&g