Skip to content

Instantly share code, notes, and snippets.

@AnatoliyLitinskiy
Created September 24, 2018 12:43
Show Gist options
  • Save AnatoliyLitinskiy/f17899305cb8dd350f44efbf8bbb8b33 to your computer and use it in GitHub Desktop.
Save AnatoliyLitinskiy/f17899305cb8dd350f44efbf8bbb8b33 to your computer and use it in GitHub Desktop.
php yield
Generator
Array
(
[0] => rewind
[1] => valid
[2] => current
[3] => key
[4] => next
[5] => send
[6] => throw
[7] => getReturn
[8] => __wakeup
)
true
0.txt
current '0'
current '1'
0.txt
1.txt
current '2'
0.txt
1.txt
2.txt
current '3'
0.txt
1.txt
2.txt
3.txt
current '4'
0.txt
1.txt
2.txt
3.txt
4.txt
current '5'
0.txt
1.txt
2.txt
3.txt
4.txt
5.txt
current ''
0.txt
1.txt
2.txt
3.txt
4.txt
5.txt
6.txt
current ''
0.txt
1.txt
2.txt
3.txt
4.txt
5.txt
6.txt
current ''
0.txt
1.txt
2.txt
3.txt
4.txt
5.txt
6.txt
class TestYield
{
private function clearDir() {
if (!is_dir('./files')) {
return;
}
if ($handle = opendir('./files')) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..") {
// echo "$entry\n";
unlink("./files/{$entry}");
}
}
closedir($handle);
}
}
private function listDir() {
if (!is_dir('./files')) {
return;
}
if ($handle = opendir('./files')) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..") {
echo "$entry\n";
}
}
closedir($handle);
}
}
private function getGenerator()
{
if (!is_dir('./files')) {
mkdir('./files', 0777);
}
$i = 0;
file_put_contents('./files/'.$i.'.txt', "content $i");
yield $i;
$i++;
file_put_contents('./files/'.$i.'.txt', "content $i");
yield $i;
$i++;
file_put_contents('./files/'.$i.'.txt', "content $i");
yield $i;
$i++;
file_put_contents('./files/'.$i.'.txt', "content $i");
yield $i;
$i++;
file_put_contents('./files/'.$i.'.txt', "content $i");
yield $i;
$i++;
file_put_contents('./files/'.$i.'.txt', "content $i");
yield $i;
$i++;
file_put_contents('./files/'.$i.'.txt', "content $i");
}
public function run()
{
$this->clearDir();
$generator = $this->getGenerator();
echo "\n";
print_r(get_class($generator));
echo "\n";
print_r(get_class_methods($generator));
echo "\n";
$this->listDir();
echo $generator->valid() ? "true\n" : "false\n";
$this->listDir();
echo "current '{$generator->current()}'\n";
$generator->next();
echo "\n\n\n";
echo "current '{$generator->current()}'\n";
$this->listDir();
// echo $generator->valid() ? "true\n" : "false\n";
$generator->next();
echo "\n\n\n";
echo "current '{$generator->current()}'\n";
$this->listDir();
// echo $generator->valid() ? "true\n" : "false\n";
$generator->next();
echo "\n\n\n";
echo "current '{$generator->current()}'\n";
$this->listDir();
// echo $generator->valid() ? "true\n" : "false\n";
$generator->next();
echo "\n\n\n";
echo "current '{$generator->current()}'\n";
$this->listDir();
// echo $generator->valid() ? "true\n" : "false\n";
$generator->next();
echo "\n\n\n";
echo "current '{$generator->current()}'\n";
$this->listDir();
// echo $generator->valid() ? "true\n" : "false\n";
$generator->next();
echo "\n\n\n";
echo "current '{$generator->current()}'\n";
$this->listDir();
// echo $generator->valid() ? "true\n" : "false\n";
$generator->next();
echo "\n\n\n";
echo "current '{$generator->current()}'\n";
$this->listDir();
// echo $generator->valid() ? "true\n" : "false\n";
$generator->next();
echo "\n\n\n";
echo "current '{$generator->current()}'\n";
$this->listDir();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment