Modern PHP interface 接口
接口 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 接口的更多相关文章
- as3.0 interface接口使用方法
[转]as3.0 interface接口使用方法 AS在2.0的时候就支持接口了 接口能够让你的程序更具扩展性和灵活性,打个例如 比方你定义了一个方法 代码: public function aMet ...
- interface接口
当一个抽象类中的方法都是抽象的时候,这时可以将该抽象类用另一种形式定义和表示,就是接口 interface. 定义接口使用的关键字不是class,是interface.接口中常见的成员: 这些成员都有 ...
- golang面向对象和interface接口
一. golang面向对象介绍 1.golang也支持面向对象编程,但是和传统的面向对象编程有区别,并不是纯粹的面向对象语言.2.golang没有类(class),golang语言的结合体(struc ...
- Golang 入门系列(四)如何理解interface接口
前面讲了很多Go 语言的基础知识,包括go环境的安装,go语言的语法等,感兴趣的朋友,可以先看看之前的文章.https://www.cnblogs.com/zhangweizhong/category ...
- go interface接口
一:接口概要 接口是一种重要的类型,他是一组确定的方法集合. 一个接口变量可以存储任何实现了接口方法的具体值.一个重要的例子就是io.Reader和io.Writer type Reader inte ...
- java interface接口的传值方法
A 类 package interface_test; public class A { private IPresenter ip; public A(IPresenter ip) { this.i ...
- JAVA 构造器, extends[继承], implements[实现], Interface[接口], reflect[反射], clone[克隆], final, static, abstrac
记录一下: 构造器[构造函数]: 在java中如果用户编写类的时候没有提供构造函数,那么编译器会自动提供一个默认构造函数.它会把所有的实例字段设置为默认值:所有的数字变量初始化为0;所有的布尔变量设置 ...
- 011-对象——interface接口说明与使用方式实例
<?php /** interface接口说明与使用方式实例 * * 接口里面的方法全是抽象方法,没有实体的方法.这样的类我们就叫做接口.定义的时候用Interface定义.实现接口时用impl ...
- Java Interface接口
Java 中接口概念 接口可以理解为一种特殊的 类,由 全局常量 和 公共的抽象方法 所组成. 类是一种具体实现体,而接口定义了某一批类所需要遵循的规范,接口不关心这些类的内部数据, 也不关心这些类里 ...
随机推荐
- Linux 进程间传递文件描述符
文章目录 文件描述符 文件数据结构 共享文件 UNIX域socket实现传递文件描述符 进程间传递打开的文件描述符,并不是传递文件描述符的值.先说一下文件描述符. 文件描述符 对内核来说,所有打开的文 ...
- JVM 基础面试题总结
hey guys, 各位小伙伴们大家好,这里是程序员cxuan,欢迎你收看我新一期的文章,这篇文章我花了几天时间给你汇总了一波 JVM 的基础知识和面试题,内容还不是很全,我还在连载中,这篇文章相当于 ...
- Windows内核基础知识-1-段寄存器
Windows内核基础知识-1-段寄存器 学过汇编的应该都知道段寄存器,在Windows里段寄存器有很多,之前可能只接触了ds数据段,cs 代码段这种,今天这个博客就介绍Windows一些比较常用的段 ...
- Avro使用手册
1. Overview Data serialization is a technique of converting data into binary or text format. There a ...
- NB-IoT四大关键特性及实现告诉你,为啥NB
摘要:NB- IoT 网络是基于4G网络演进过来的,所以它在上行和下行的复用技术上还是沿用了4G的OFDMA和SC-FDMA. 本文分享自华为云社区<一文了解NB-IoT四大关键特性以及实现技术 ...
- 【设计模式】DDD 设计理念
From: https://liudongdong1.github.io/ 微服务架构,在集中式架构中,系统分析.设计和开发往往是独立进行的,而且各个阶段负责人可能不一样,那么就涉及到交流信息丢失的问 ...
- COM笔记-CoCreateInstance
CoCreateInstance 创建组件的最简单的方法是使用CoCreateInstance函数. 在COM库中包含一个用于创建组件的名为CoCreateInstance的函数.此函数需要一个CLS ...
- long ? 的使用和理解
Dictionary<string, object> dic = new Dictionary<string, object>(); long lg = 12345; dic[ ...
- Mybatis映射器(二)
上一篇文章返回是resultType,但其无法定义多的属性,比如typeHandler,级联等.为了支持复杂映射,可以用resultMap属性,先定义resultmap属性: <mapper n ...
- ProjectEuler 008题
题目: The four adjacent digits in the 1000-digit number that have the greatest product are 9 9 8 9 = 5 ...