生成器委托
簡單地翻譯官方文檔的描述:
PHP7中,通過生成器委托(yield from),可以將其他生成器、可迭代的對象、數組委托給外層生成器。外層的生成器會先順序 yield 委托出來的值,然后繼續 yield 本身中定義的值。
利用 yield from 可以方便我們編寫比較清晰生成器嵌套,而代碼嵌套調用是編寫復雜系統所必需的。
上例子:
~~~
<?php
function echoTimes($msg, $max) {
for ($i = 1; $i <= $max; ++$i) {
echo "$msg iteration $i\n";
yield;
}
}
function task() {
yield from echoTimes('foo', 10); // print foo ten times
echo "---\n";
yield from echoTimes('bar', 5); // print bar five times
}
foreach (task() as $item) {
;
}
~~~
以上將輸出:
~~~
foo iteration 1
foo iteration 2
foo iteration 3
foo iteration 4
foo iteration 5
foo iteration 6
foo iteration 7
foo iteration 8
foo iteration 9
foo iteration 10
---
bar iteration 1
bar iteration 2
bar iteration 3
bar iteration 4
bar iteration 5
~~~
自然,內部生成器也可以接受它的父生成器發送的信息或者異常,因為 yield from 為父子生成器建立一個雙向的通道。不多說,上例子:
~~~
<?php
function echoMsg($msg) {
while (true) {
$i = yield;
if($i === null){
break;
}
if(!is_numeric($i)){
throw new Exception("Hoo! must give me a number");
}
echo "$msg iteration $i\n";
}
}
function task2() {
yield from echoMsg('foo');
echo "---\n";
yield from echoMsg('bar');
}
$gen = task2();
foreach (range(1,10) as $num) {
$gen->send($num);
}
$gen->send(null);
foreach (range(1,5) as $num) {
$gen->send($num);
}
//$gen->send("hello world"); //try it ,gay
~~~
輸出和上個例子是一樣的。
生成器返回值
如果生成器被迭代完成,或者運行到 return 關鍵字,是會給這個生成器返回值的。
可以有兩種方法獲取這個返回值:
使用 $ret = Generator::getReturn() 方法。
使用 $ret = yield from Generator() 表達式。
上例子:
~~~
<?php
function echoTimes($msg, $max) {
for ($i = 1; $i <= $max; ++$i) {
echo "$msg iteration $i\n";
yield;
}
return "$msg the end value : $i\n";
}
function task() {
$end = yield from echoTimes('foo', 10);
echo $end;
$gen = echoTimes('bar', 5);
yield from $gen;
echo $gen->getReturn();
}
foreach (task() as $item) {
;
}
~~~
輸出結果就不貼了,想必大家都猜到。
可以看到 yield from 和 return 結合使得 yield 的寫法更像平時我們寫的同步模式的代碼了,畢竟,這就是 PHP 出生成器特性的原因之一呀。
一個非阻塞的web服務器
時間回到2015年,鳥哥博客上轉載的一篇《 在PHP中使用協程實現多任務調度》。文章介紹了PHP5 的迭代生成器,協程,并實現了一個簡單的非阻塞 web 服務器。
現在我們利用 PHP7 中的這兩個新特性重寫這個 web 服務器,只需要 100 多行代碼。
代碼如下:
~~~
<?php
class CoSocket
{
protected $masterCoSocket = null;
public $socket;
protected $handleCallback;
public $streamPoolRead = [];
public $streamPoolWrite = [];
public function __construct($socket, CoSocket $master = null)
{
$this->socket = $socket;
$this->masterCoSocket = $master ?? $this;
}
public function accept()
{
$isSelect = yield from $this->onRead();
$acceptS = null;
if ($isSelect && $as = stream_socket_accept($this->socket, 0)) {
$acceptS = new CoSocket($as, $this);
}
return $acceptS;
~~~
- OAuth
- 簡介
- 步驟
- 單點登錄
- .user.ini
- 時間轉換為今天昨天前天幾天前
- 獲取ip接口
- 協程
- 概念
- yield-from && return-values
- 協程與阻塞的思考
- 中間件
- mysqli異步與php的協程
- 代碼片段
- pdo 執行的sql語句
- 二進制安全
- 捕捉異常中斷
- global
- 利用cookie模擬登陸
- 解析非正常json
- 簡單的對稱加密算法
- RSA 加密
- 過濾掉emoji表情
- 判斷遠程圖片是否存在
- 一分鐘限制請求100次
- 文件處理
- 多文件上傳
- 顯示所有文件
- 文件下載和上面顯示所有文件配合
- 文件的刪除,統計,存數組等
- 圖片處理
- 簡介
- 驗證碼
- 圖片等比縮放
- 批量添加水印
- beanstalkd
- 安裝
- 使用
- RabbitMQ
- 簡介
- debain安裝
- centos安裝
- 常用方法
- 入門
- 工作隊列
- 訂閱,發布
- 路由
- 主題
- 遠程調用RPC
- 消息中間件的選型
- .htaccess
- isset、empty、if區別以及0、‘’、null
- php各版本
- php7.2 不向后兼容的改動
- php中的各種坑
- php7改變
- php慢日志
- 郵件
- PHPMailer實現發郵件
- 驗證郵件地址真實性
- 文件下載
- FastCgi 與 PHP-fpm 之間的關系
- openssl 加解密
- 反射
- 鉤子方法
- 查找插件
- opcode
- opcache使用
- opcache優化
- 分布式一致性hash算法
- 概念
- 哈希算法好壞的四個定義
- php實現
- java實現
- 數組
- jwt
- jwt簡介
- 單點登錄
- phpize
- GeoIP擴展
- php無法獲得https網頁內容的解決方案
- homestead運行的腳本
- Unicode和Utf-8轉換
- php優化
- kafka
- fpm配置
- configure配置詳解