krot

yield

Mar 21st, 2018
111
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. function ff($e){
  4.  
  5.     for($i=0;$i<$e;++$i){
  6.     yield $i;
  7.     }
  8. }
  9.  
  10. $iter=ff(11);
  11. foreach($iter as $v)..
  12.  
  13. while($iter->valid()){
  14. echo $iter->current();
  15. $iter->next();
  16. }
  17. ...
  18. function logger($fileName) {
  19.     $fileHandle = fopen($fileName, 'a');
  20.     while (true) {
  21.         fwrite($fileHandle, yield . "\n");
  22.     }
  23. }
  24.  
  25. $logger = logger(__DIR__ . '/log');
  26. $logger->send('Foo');
  27. $logger->send('Bar');
  28.  
  29. function html($fileName='php://output'){
  30.      
  31.     $h = fopen($fileName, 'w');
  32.    
  33.      try {
  34.     fwrite($h,'@start');
  35.    
  36.    
  37.     fwrite($h, (yield 'ok'));
  38.     fwrite($h,'@end');
  39.    
  40.     } finally {
  41.         fclose($h);
  42.     }
  43.    
  44.    
  45. }
  46.  
  47. $i=html( );
  48.  
  49. $msg=$i->current();
  50. $i->send($msg.':'. 123);
RAW Paste Data