(三)PHP面向对象理论3---接口与反射

接口:采用一个特定接口的所有代码都知道对于那个接口会调用什么方法。反射:反射是在PHP运行状态中,扩展分析PHP程序,导出或者提取出关于类、方法、属性、参数等的详细信息,
摘要由CSDN通过智能技术生成
一. 接口:
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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值