Tips for using PHP interfaces

Source: Internet
Author: User


absrtact: in the process of programming we should learn how to use the interface to change our lives, greatly enhance the ability of self. Interface is not a new feature, but very important, let's take a small example of the interface. ...

Fictitious a Documentstore class, this class is responsible for collecting text from different resources. You can read HTML from a remote URL, read a resource, or collect terminal command output.

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

Since the parameters of the Adddocument () method can only be instances of documenttable classes, how can you define Documentstore classes? In fact, documenttable is not a class, it is an interface;

interface documenttable{Public Function getId ();    Public function getcontent (); }

This interface defines the table name, and any object that implements the Documenttable interface must provide a public getid () method and a public getcontent () method.

> But what's the use of doing this? The advantage of this is that we can define the classes that are stable, and we can use very different methods. The following is a way to get HTML from a remote URL using curl.

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 = new Documentstore ();//Add HTML Document $htmldoc = new HTMLDocument (' https://www.i360.me '); $documentStore- >adddocument ($HTMLDOC);//Add flow document $streamdoc = new Streamdocument (fopen (' stream.txt ', ' RB ')); $documentStore Adddocument ($streamDOC);//Add terminal command Document $cmddoc = new Commandoutdocument (' cat/etc/hosts '); $documentStore Adddocument ($command);p Rint_r ($documentStore->getdocuments ());d ie;

Here Htmldocument,streamdocument,commandoutdocument these three classes have nothing in common, just implement the same interface.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.