什么是接口?

接口是两个PHP对象之间的契约,其目的不是让一个对象依赖另一个对象,而是依赖一个对象的能力。
接口把我们的代码和依赖解耦了,而且允许我们的代码任何实现了预期接口的第三方代码。我们不管第三方代码是如何实现接口的,只关心第三方代码是否实现了指定的接口。


定义一个DocumentStore
作用是从不同的源收集文本:可以从远程URL读取HTML,也可以读取流资源,也可以收集终端的输出

class DocumentStore 
{
    protected $data = [];

    public function addDocument(Documentable $document)
    {
        $key   = $document->getId();
        $value = $document->getContent();
        $this->data[$key] = $value;
    }

    public function getDocuments()
    {
        return $this->data;
    }    
}

定义Documentable接口

interface Documentable
{
    public function getId();
    public function getContent();
}

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

这么做的用处是,我们可以分开定义获取文档的类,而且使用十分不同的实现方法

//使用curl从远程URL获取HTML
class HtmlDocument implements Documentable
{
    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, 1);
        $html = curl_exec($ch);
        curl_close($ch);

        return $html;
    
//读取资源流
class StreamDocument implements Documentable
{
    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 CommandOutputDocument implements Documentable
{
    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://php.net');
$documentStore->addDocument($htmlDoc);

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

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

print_r($documentStore->getDocuments());

HtmlDocumentStreamDocumentCommandOutputDocument三个类没任何共同点,只是实现了一个接口

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值