The right way

/dev/hell Code

Response.php

接口 demo:

modern-php/
├── data
│   └── stream.txt
└── interface
├── CommandOutputDocument.php
├── DocumentStore.php
├── Documentable.php
├── HtmlDocument.php
├── StreamDocument.php
└── index.php

// interface

<?php

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

Documentable.php

//  class x 3 implements interface

<?php
require_once __DIR__ . '/Documentable.php'; class HtmlDocument implements Documentable {
protected $url; public function __construct($url) {
$this->url = $url;
} public function getId() {
return $this->url;
} /**
* @return string
*/
public function getContent() {
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => $this->url,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_CONNECTTIMEOUT => 3,
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_MAXREDIRS => 3
]);
$html = curl_exec($ch);
curl_close($ch);
return $html;
}
}

HtmlDocument.php

 1 <?php
2 require_once __DIR__ . '/Documentable.php';
3
4 class StreamDocument implements Documentable {
5
6 protected $resource;
7 protected $buffer;
8
9 public function __construct($resource, $buffer = 4096) {
10 $this->resource = $resource;
11 $this->buffer = $buffer;
12 }
13
14 public function getId() {
15 return 'resource-' .(int)$this->resource;
16 }
17
18 public function getContent() {
19 $streamContent = '';
20 rewind($this->resource);
21 while (feof($this->resource) === false) {
22 $streamContent .= fread($this->resource, $this->buffer);
23 }
24 return $streamContent;
25 }
26 }

StreamDocument.php

 1 <?php
2 require_once __DIR__ . '/Documentable.php';
3
4 class CommandOutputDocument implements Documentable {
5
6 protected $command;
7
8 public function __construct($command) {
9 $this->command = $command;
10 }
11
12 public function getId() {
13 return $this->command;
14 }
15
16 public function getContent() {
17 return shell_exec($this->command);
18 }
19 }

CommandOutputDocument.php

// Container

 1 <?php
2 include 'Documentable.php';
3
4 class DocumentStore {
5 protected $data = [];
6
7 public function addDocument(Documentable $document) {
8 $key = $document->getId();
9 $value = $document->getContent();
10 $this->data[$key] = $value;
11 }
12
13 public function getDocuemnts() {
14 return $this->data;
15 }
16 }

DocumentStore.php

// Usage:

<?php
include 'DocumentStore.php'; include 'HtmlDocument.php';
include 'StreamDocument.php';
include 'CommandOutputDocument.php'; $documentStore = new DocumentStore(); $htmlDoc = new HtmlDocument('https://php.net');
$documentStore->addDocument($htmlDoc); $streamDoc = new StreamDocument(fopen('../data/stream.txt', "rb"));
$documentStore->addDocument($streamDoc); $cmdDoc = new CommandOutputDocument('cat /etc/hosts');
$documentStore->addDocument($cmdDoc); print_r($documentStore->getDocuemnts());

index.php

Modern PHP interface 接口的更多相关文章

  1. as3.0 interface接口使用方法

    [转]as3.0 interface接口使用方法 AS在2.0的时候就支持接口了 接口能够让你的程序更具扩展性和灵活性,打个例如 比方你定义了一个方法 代码: public function aMet ...

  2. interface接口

    当一个抽象类中的方法都是抽象的时候,这时可以将该抽象类用另一种形式定义和表示,就是接口 interface. 定义接口使用的关键字不是class,是interface.接口中常见的成员: 这些成员都有 ...

  3. golang面向对象和interface接口

    一. golang面向对象介绍 1.golang也支持面向对象编程,但是和传统的面向对象编程有区别,并不是纯粹的面向对象语言.2.golang没有类(class),golang语言的结合体(struc ...

  4. Golang 入门系列(四)如何理解interface接口

    前面讲了很多Go 语言的基础知识,包括go环境的安装,go语言的语法等,感兴趣的朋友,可以先看看之前的文章.https://www.cnblogs.com/zhangweizhong/category ...

  5. go interface接口

    一:接口概要 接口是一种重要的类型,他是一组确定的方法集合. 一个接口变量可以存储任何实现了接口方法的具体值.一个重要的例子就是io.Reader和io.Writer type Reader inte ...

  6. java interface接口的传值方法

    A 类 package interface_test; public class A { private IPresenter ip; public A(IPresenter ip) { this.i ...

  7. JAVA 构造器, extends[继承], implements[实现], Interface[接口], reflect[反射], clone[克隆], final, static, abstrac

    记录一下: 构造器[构造函数]: 在java中如果用户编写类的时候没有提供构造函数,那么编译器会自动提供一个默认构造函数.它会把所有的实例字段设置为默认值:所有的数字变量初始化为0;所有的布尔变量设置 ...

  8. 011-对象——interface接口说明与使用方式实例

    <?php /** interface接口说明与使用方式实例 * * 接口里面的方法全是抽象方法,没有实体的方法.这样的类我们就叫做接口.定义的时候用Interface定义.实现接口时用impl ...

  9. Java Interface接口

    Java 中接口概念 接口可以理解为一种特殊的 类,由 全局常量 和 公共的抽象方法 所组成. 类是一种具体实现体,而接口定义了某一批类所需要遵循的规范,接口不关心这些类的内部数据, 也不关心这些类里 ...

随机推荐

  1. 内存吞金兽(Elasticsearch)的那些事儿 -- 认识一下

    背景及常见术语 背景 Elasticsearch 是一个开源的搜索引擎,建立在一个全文搜索引擎库 Apache Lucene 基础之上. Lucene 可以说是当下最先进.高性能.全功能的搜索引擎库- ...

  2. 微软官方安装介质Windows10系统安装教程

    微软官方安装介质Windows10系统安装教程 Jasper游戏 发布时间:04-2204:23 小贴士:事前准备 ★ 拥有 Internet 连接 ★ 在计算机.USB 或外部驱动器上拥有足够的可用 ...

  3. Jmeter 生成测试报告、Jenkins 配置

    1. Jmeter 生成测试报告  示例: jmeter -n -t test.jmx -l result.jtl -e -o ./report 成功执行并生成报告: 生成报告失败:注意报告存放目录或 ...

  4. 【多线程】不懂什么是 Java 中的锁?看看这篇你就明白了!

    本文来源:Java建设者 原文地址:https://mp.weixin.qq.com/s/GU42BjM5jY2CEMVD_PAZBQ Java 锁分类 Java 中的锁有很多,可以按照不同的功能.种 ...

  5. uwp 之语音识别

    xml code ---------------------------------------------- <Page x:Class="MyApp.MainPage" ...

  6. 【springcloud】Zuul 超时、重试、并发参数设置

    转自:https://blog.csdn.net/xx326664162/article/details/83625104 一. Zuul 服务网关 服务网关 = 路由转发 + 过滤器 1.路由转发: ...

  7. C++ 各种构造函数

    c++构造函数的知识在各种c++教材上已有介绍,不过初学者往往不太注意观察和总结其中各种构造函数的特点和用法,故在此我根据自己的c++编程经验总结了一下c++中各种构造函数的特点,并附上例子,希望对初 ...

  8. IO异常--缓冲流--转换流--序列化流( IO流2 )

    1.IO异常的处理 JDK7前处理:使用try...catch...finally 代码块,处理异常部分 // 声明变量 FileWriter fw = null; try { //创建流对象 fw ...

  9. docker 安装Hive

    转自:https://www.cnblogs.com/upupfeng/p/13452385.html#%E9%83%A8%E7%BD%B2hive 使用docker快速搭建hive环境   记录一下 ...

  10. Java HdAcm1174

    空间一般直线的方程是:(x-x0)/a=(y-y0)/b=(z-z0)/c,这是一条过(x0,y0,z0),方向矢量为{a,b,c}的直线.假设已知点的坐标是A(e,f,g),过A点,且与{a,b,c ...