ホームページ  >  記事  >  バックエンド開発  >  PHP インターフェースの使用に関するヒント

PHP インターフェースの使用に関するヒント

巴扎黑
巴扎黑オリジナル
2017-08-17 09:02:011886ブラウズ


要約: プログラミングの過程で、私たちはインターフェースを使用して生活を変え、自己能力を大幅に向上させる方法を学ぶ必要があります。インターフェイスは新しい機能ではありませんが、非常に重要です。インターフェイスの小さな例を示します。 ...

さまざまなリソースからテキストを収集する役割を担う 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 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。