Tips on using PHP interface

巴扎黑
Release: 2023-03-15 10:32:01
Original
1943 people have browsed it


Abstract: In the process of programming, we should learn how to use interfaces to change our lives and greatly improve our self-ability. Interfaces are not a new feature, but they are very important. Let’s give a small example of interfaces. ...

Imagine a DocumentStore class, which is responsible for collecting text from different resources. You can read HTML from remote URLs, read resources, and collect terminal command output.

Define the DocumentStore class

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;
    }
    
}
Copy after login

Since the parameters of the addDocument() method can only be instances of the Documenttable class, how can we define the DocumentStore class in this way? In fact, Documenttable is not Class is an interface;

define Documenttable

interface Documenttable{   
     public function getId(); 
        public function getContent(); 
   
}
Copy after login

This interface defines the table name. Any object that implements the Documenttable interface must provide a public getId() method and a Public getContent() method.

可是这么做有什么用呢?这么做的好处就是,我们可以分开定义获取稳定的类,而且能使用十分不同的方法。下面是一种实现方式,这种方式使用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;
    }
}
Copy after login

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

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;
    }
}
Copy after login

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

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);
    }

}
Copy after login

下面我们来演示一下借助上面的三个类来实现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;
Copy after login

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

The above is the detailed content of Tips on using PHP interface. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template