PHP interface(接口)的示例代码
- <?php
- 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;
- }
- }
- interface Documentable
- {
- public function getId();
- public function getContent();
- }
- 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, 3);
- $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 = new DocumentStore();
- //添加HTML文档
- $htmlDoc = new HtmlDocument('https://php.net');
- $documentStore->addDocument($htmlDoc);
- //添加流文档
- $streamDoc = new StreamDocument(fopen('stream.txt', 'rb'));
- $documentStore->addDocument($streamDoc);
- //添加终端命令文档
- $cmdDoc = new CommandOutputDocument('cat /etc/hosts');
- $documentStore->addDocument($cmdDoc);
- print_r($documentStore->getDocuments());
PHP interface(接口)的示例代码的更多相关文章
- java对接申通下单接口示例代码
上面是控制台示例代码 public class Sample{ private final static String URL = "http://order.sto-express.cn: ...
- wechat开发笔记之1.接口示例代码
修改后的php示例代码! <?php /** * wechat php test */ //define your token define("TOKEN", "w ...
- C/C++ 开源库及示例代码
C/C++ 开源库及示例代码 Table of Contents 说明 1 综合性的库 2 数据结构 & 算法 2.1 容器 2.1.1 标准容器 2.1.2 Lockfree 的容器 2.1 ...
- 左右JAVA示例代码事件分发和监督机制来实现-绝对原创有用
文章标题:左右JAVA示例代码事件分发和监督机制来实现 文章地址: http://blog.csdn.net/5iasp/article/details/37054171 作者: javaboy201 ...
- 【Android应用开发】 Universal Image Loader ( 使用简介 | 示例代码解析 )
作者 : 韩曙亮 转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/50824912 相关地址介绍 : -- Universal I ...
- go interface接口
一:接口概要 接口是一种重要的类型,他是一组确定的方法集合. 一个接口变量可以存储任何实现了接口方法的具体值.一个重要的例子就是io.Reader和io.Writer type Reader inte ...
- JDK静态代理示例代码
JDK静态代理示例代码 业务接口 接口的实现类 代理类,实现接口,并扩展实现类的功能 1.业务接口 /** * 业务接口 * @author pc * */ public interface User ...
- 在ASP.NET Web API 2中使用Owin OAuth 刷新令牌(示例代码)
在上篇文章介绍了Web Api中使用令牌进行授权的后端实现方法,基于WebApi2和OWIN OAuth实现了获取access token,使用token访问需授权的资源信息.本文将介绍在Web Ap ...
- Golang基础(8):go interface接口
一:接口概要 接口是一种重要的类型,他是一组确定的方法集合. 一个接口变量可以存储任何实现了接口方法的具体值.一个重要的例子就是io.Reader和io.Writer type Reader inte ...
- 三、从GitHub浏览Prism示例代码的方式入门WPF下的Prism之Mvvm的08-12示例
这一篇是学习了前2篇RegionManager关联视图,和通过不同的方式加载Module示例之后的开始进入MVVM了. 从第08示例开始,进入了MVVM部分. 从08示例开始学习Prism下的MVVM ...
随机推荐
- CSS之伪类
1. :link 向未被访问的链接添加样式 :visited 向已被访问的链接添加样式 :hover ...
- AJAX发送参数到后台,前台火狐debug报undefine
后面经过查找:估计是数据并不是Json格式,由于var PATIENT_ID=getIdSelections();其中PATIENT_ID是数组,所以必须转成字符串. $('#table').on(' ...
- async 和 await小结
三大返回值: 返回类型 - Task<TResult> 返回类型 - Task 返回类型 - void 当你添加 async 关键字后,需要返回一个将用于后续操作的对象,请使用 Task& ...
- 使用jsonp跨域请求后可以获得数据,但是进入error方法,返回parseerror
$.ajax({ url:url, dataType:'jsonp', jsonp: 'callback',//回调函数名字 jsonpCallback: 'success_jsonpCallback ...
- GCD的简单用法
/* 创建一个队列用来执行任务,TA属于系统预定义的并行队列即全局队列,目前系统预定义了四个不同运行优先级的全局队列,我们可以通过dispatch_get_global_queue来获取它们 四种优先 ...
- centos7 搭建GlusterFS
centos7 搭建GlusterFS 转载http://zhaijunming5.blog.51cto.com/10668883/1704535 实验需求:4台机器安装GlusterFS组成一个集群 ...
- laravel5.1学习(1)--安装
主要学习的是laravel5.1版本,服务器用的是wampserver3.0.4集成环境: 首先,安装composer(windows系统) 下载地址:https://getcomposer.org/ ...
- margin属性
可以设置position:absolute/relative/fixed,通过调节top/bottom/left/right实现元素的定位,这样很好,但是有时候想通过margin来实现. 下面是mar ...
- yii框架的理解
Yii Framework是一个基于组件.用于开发大型 Web 应用的高性能 PHP 框架.Yii提供了今日Web 2.0应用开发所需要的几乎一切功能.Yii是最有效率的PHP框架之一. yii框架里 ...
- IOS 调用系统照相机和相册
/** * 调用照相机 */ - (void)openCamera { UIImagePickerController *picker = [[UIImagePickerController all ...