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. 单片机学习(八)点阵LED的使用

    目录 一.点阵LED的理论知识 1. 点阵屏的打开方式 2. LED点阵简介 3. 点阵LED的结构和操作方式 4. 74HC595模块 PPT上的简介 串行转并行的工作原理 二.编码实现 1. C5 ...

  2. 虚拟机下安装Centos设置静态ip,并通过桥接连接

    Centos7.6设置静态IP 1.CentOS7默认网卡设备文件存放于该目录下 /etc/sysconfig/network-scripts/ 网卡设备默认的名字为:ifcfg-ens33 查看网卡 ...

  3. VMware上安装的Ubuntu不显示全屏解决方法

    花费了好一会才把电脑上的Vmware装上,把Ubuntu装到虚拟机上,谁知道Ubuntu不显示全屏,我调了分辨率,奈何Ubuntu里面固定的分辨率没有跟我电脑匹配的,然后开始寻找解决方法,在网上找了很 ...

  4. 微信小程序中h5跳转到登录页面,登陆成功返回携带参数,h5刷新

    公司的一个小程序,要做一个活动,需要判断登录状态. 思路:h5跳转到登录页面,登陆成功携带token自动返回. 本来以为是个非常简单的功能,没想到..... 发帖记录一下 1.登录页面 用getCur ...

  5. 个人笔记-----Vue中多个router-view应用

    单个 <router-view/> 和多个 <router-view/> 的区别,单个 <router-view/> 只是一个区域的变化,不需要设置name属性,在 ...

  6. Hibernate5 入门之SessionFactory对象的创建

    hibernate5创建SessionFactory不同于hibernate4和hibernate3,下面是代码示例. package top.scorpion.util; import org.hi ...

  7. 【java文件处理】java项目路径下的文件下载处理

    1. controller类: package com.neo.controller; import javax.servlet.http.HttpServletResponse; import or ...

  8. 【mysql】关联查询_子查询_排序分组优化

    1. 关联查询优化 1.1 left join 结论: ①在优化关联查询时,只有在被驱动表上建立索引才有效! ②left join 时,左侧的为驱动表,右侧为被驱动表! 1.2 inner join ...

  9. MVVMLight学习笔记(二)---MVVMLight框架初探

    一.MVVM分层概述 MVVM中,各个部分的职责如下: Model:负责数据实体的结构处理,与ViewModel进行交互: View:负责界面显示,与ViewModel进行数据和命令的交互: View ...

  10. RabbitMq四种模式介绍和授权

    rabbitmqctl change_password admin admin123 修改admin密码 界面管理和授权操作 1新增用户 rabbitmqctl add_user admin amin ...