登录  /  注册

PHP接口的使用技巧

巴扎黑
发布: 2017-08-17 09:02:01
原创
1764人浏览过


摘要:在编程的过程中我们应该学会如何使用接口来给变我们的生活,极大的提升自我能力。接口不是新特性,但是非常重要,下面我们来撸个接口的小例子。 ...

虚构一个DocumentStore的类,这个类负责从不同的资源收集文本。可以从远程url读取html,也可以读取资源,也可以收集终端命令输出。

定义DocumentStore类

class DocumentStore{    protected $data = [];    
    public function addDocument(Documenttable $document){
        $key = $document->getId();
        $value = $document->getContent();        $this->data[key] = $value;        
    }    
    public function getDocuments(){        return $this->data;
    }
    
}
登录后复制

既然addDocument()方法的参数只能是Documenttable的类的实例,这样定义DocumentStore的类怎么行呢? 其实Documenttable不是类,是接口;

定义Documenttable

interface Documenttable{   
     public function getId(); 
        public function getContent(); 
   
}
登录后复制

这个接口定义表名,实现Documenttable接口的任何对象都必须提供一个公开的getId()方法和一个公开的getContent()方法。

可是这么做有什么用呢?这么做的好处就是,我们可以分开定义获取稳定的类,而且能使用十分不同的方法。下面是一种实现方式,这种方式使用curl从远程url获取html。

定义HtmlDocument类

class HtmlDocument implements Documenttable{    protected $url;    public function __construct($url)
    {        $this->url = $url;
    }    public function getId(){        return $this->url;
    }    public function getContent(){
        $ch = curl_init();
        curl_setopt($ch,CURLOPT_URL,$this->url);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,3);
        curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
        curl_setopt($ch,CURLOPT_MAXREDIRS,3);
        curl_close($ch);        return $thml;
    }
}
登录后复制

下面一个方法是获取流资源。

class StreamDocument implements Documenttable{    protected $resource;    protected $buffer;    public function __construct($resource,$buffer = 4096)
    {        $this->resource=$resource;        $this->buffer=$buffer;
    }    public function getId(){        return 'resource-' .(int)$this->resource;
    }    public function getContent(){
        $streamContent = '';
        rewind($this->resource);        while (feof($this->resource) === false){
            $streamContent .= fread($this->resource,$this->buffer);
        }        return $streamContent;
    }
}
登录后复制

下面一个类是获取终端命令行的执行结果。

class CommandOutDocument implements Documenttable{    protected $command;    public function __construct($command)
    {        $this->command=$command;
    }    
    public function getId(){        return $this->command;
    }    public function getContent(){        return shell_exec($this->command);
    }

}
登录后复制

下面我们来演示一下借助上面的三个类来实现DocumentStore类。

$documentStore = new DocumentStore();//添加html文档$htmlDoc = new HtmlDocument('https:// www.i360.me');

$documentStore->addDocument($htmlDoc);//添加流文档$streamDOC = new StreamDocument(fopen('stream.txt','rb'));

$documentStore->addDocument($streamDOC);//添加终端命令文档$cmdDoc = new CommandOutDocument('cat /etc/hosts');

$documentStore->addDocument($command);

print_r($documentStore->getDocuments());die;
登录后复制

这里HtmlDocument,StreamDocument,CommandOutDocument这三个类没有任何共同点,只是实现了同一个接口。

以上就是PHP接口的使用技巧的详细内容,更多请关注php中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
关于CSS思维导图的课件在哪? 课件
凡人来自于2024-04-16 10:10:18
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号