|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #51460 yield syntax construct
Submitted: 2010-04-02 01:37 UTC Modified: 2012-09-01 17:57 UTC
Avg. Score:4.5 ± 0.9
Reproduced:2 of 2 (100.0%)
Same Version:1 (50.0%)
Same OS:1 (50.0%)
From: a at b dot c dot de Assigned: nikic (profile)
Status: Closed Package: *Programming Data Structures
PHP Version: 5.3.2 OS: Irrelevant
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: a at b dot c dot de
New email:
PHP Version: OS:


 [2010-04-02 01:37 UTC] a at b dot c dot de
I draw your attention to the "yield" construct as used in Python, (recent) JavaScript, and C#. This would be I think a nice way of creating Iterators for traversing complex objects.

A method that uses "yield" can be replaced by one that declares an instance of a custom implementation of Iterator that takes $this and the method's arguments in its constructor, and cranks through a state machine on each call of next(). (Microsoft's C# compiler handles it in essentially this way, but has a much harder time of it because of things like multithreading and explicit resource disposal.)

To some extent this can be done by hand now (I've done some work on a preprocessing script that automates this) but in the absence of inner classes (which is what this Iterator ought to be) - hint, hint - it plays havoc with visibility attributes.

Expected result:
Example method that produces an inorder traversal of a binary tree.
foreach($btree->traverse_inorder() as $node)
public function traverse_inorder()
	foreach($this->left->traverse_inorder() as $child)
		yield $child;
	yield $this;
	foreach($this->right->traverse_inorder() as $child)
		yield $child;

Actual result:
The state machine that implements the body of the test script's function (identifies if the end of the traversal has been reached, and obtains the next value to return if not)
	case -1:
		return false;
	case 0:
		// the object being traversed needs "left" made public
		$this->_t1 = $this->_this->left->traverse_inorder();
		$this->_state = 1;
	case 1:
		if(!($e = each($this->_t1)))
			$this->_t1 = null;
			$this->_state = 2;
		$this->_value = $e['value'];
		return true;
	case 2:
		$this->_value = $this->_this;
		$this->_state = 3;
		return true;
	case 3:
		// the object being traversed needs "right" made public
		$this->_t2 = $this->_this->right->traverse_inorder();
		$this->_state = 4;
	case 4:
		if(!($e = each($this->_t2)))
			$this->_t2 = null;
			$this->_state = -1;
			return false;
		$this->_value = $e['value'];
		return true;
} while(false);


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2012-06-05 22:32 UTC]
-Status: Open +Status: Assigned -Assigned To: +Assigned To: nikic
 [2012-09-01 17:57 UTC]
This is now implemented. For more info see
 [2012-09-01 17:57 UTC]
-Status: Assigned +Status: Closed
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 12 22:01:29 2024 UTC