验证中...
Languages: PHP
Categories: 其他
Latest update 2019-01-19 08:01
PHP的生成器、yield和协程(2)
Raw Copy
//实际上生成器函数返回的是一个Generator对象,这个对象不能通过new实例化,并且实现了Iterator接口。
//http://www.laruence.com/2015/05/28/3038.html
//https://www.cnblogs.com/tingyugetc/p/6347286.html
Generator implements Iterator {
public mixed current(void)
public mixed key(void)
public void next(void)
public void rewind(void)
// 向生成器传入一个值
public mixed send(mixed $value)
public void throw(Exception $exception)
public bool valid(void)
// 序列化回调
public void __wakeup(void)
}
//test 1
function printer() {
while(true) {
echo 'receive: ' . yield . "\n";
}
}
$printer = printer();
$printer->send('Hello');
$printer->send('world');
//test2
function printer() {
$i = 1;
while(true) {
echo 'this is the yield ' . $i . "\n";
echo 'receive: ' . yield . "\n";
$i++;
}
}
$printer = printer();
$printer->send('Hello');
$printer->send('world');

Comment list( 0 )

You need to Sign in for post a comment

Help Search

183227_9af5e6a8_1826025 111910_4d91f001_1826025