17044

configure own iterator for class php?

Question:

I have a class Foo, I need to do :

$foo = new Foo(); foreach($foo as $value) { echo $value; }

and define my own method to iterate with this object, exemple :

class Foo { private $bar = [1, 2, 3]; private $baz = [4, 5, 6]; function create_iterator() { //callback to the first creation of iterator for this object $this->do_something_one_time(); } function iterate() { //callback for each iteration in foreach return $this->bar + $this->baz; } }

Can we do that? How?

Answer1:

You need to implement the <a href="http://php.net/manual/fr/class.iterator.php" rel="nofollow">\Iterator</a> or <a href="http://php.net/manual/fr/class.iteratoraggregate.php" rel="nofollow">\IteratorAggregate</a> interface to achieve that.

A simple example of what you're trying to achieve using the \IteratorAggregate and \Iterator interfaces (I've left out the \Iterator implementation details, but you can use the PHP doc to see how they work) :

class FooIterator implements \Iterator { private $source = []; public function __construct(array $source) { $this->source = $source; // Do whatever else you need } public function current() { ... } public function key() { ... } public function next() { // This function is invoked on each step of the iteration } public function rewind() { ... } public function valid() { ... } } class Foo implements \IteratorAggregate { private $bar = [1, 2, 3]; private $baz = [4, 5, 6]; public function getIterator() { return new FooIterator(array_merge($this->bar, $this->baz)); } } $foo = new Foo(); foreach ($foo as $value) { echo $value; }

Answer2:

You need to implement the <a href="http://php.net/manual/en/class.iterator.php" rel="nofollow">Iterator</a> interface.

class Foo implements Iterator {

You should review the built in interfaces:

<a href="http://php.net/manual/en/reserved.interfaces.php" rel="nofollow">http://php.net/manual/en/reserved.interfaces.php</a>

Recommend

  • ExtJS 6 plugin.rowwidget Get binded record upon row body component creation
  • Using SQLite with Phonegap Desktop
  • How to add elements in array?
  • Django - Changing inline formset textInput size attribute
  • Design Paradigm for instantiating object from XML file
  • AngularJs unit test - Check if “Init” function was called
  • Why Open Redirect URLS?
  • What's the difference between using a heading tag and a large font in a tag?
  • Opening multiple instances of an MTrimmedWindow complete with perspectives etc
  • How to check programatically that 8.3 short path name is enabled on system?
  • Does progress bar slows down application?
  • DocumentDB client lifeteme
  • Xcode is auto-adding an info icon / “Accessory Button” to my table cells
  • Index of vector iterator
  • SoX running slow using a ProcessBuilder
  • How to check when a method is run in another class in Java (method call listener)?
  • java.lang.NullPointerException: No FileItemFactory has been set
  • Reusing the CQ5 Form into the mywebsite components is not showing up the End of the Form section
  • check if numbers have the same sign
  • Load balanced Fiware Orion
  • Sorting a HashMap, while keeping duplicates
  • How to add an object in my collection by only using add method? [closed]
  • C++/CLI Thread synchronization including managed and unmanaged code
  • Iterate twice through a DataReader
  • Simplify where clause with repeated associated type restrictions
  • Create registry key in 32-bit hive on x64 PC using Installshield 2012 LE - Avoid redirection
  • pythonanywhere - How do I use websockets to transmit messages as per the web2py messaging example?
  • Defined variables not working in javascript files when I use getScript
  • Can't remove headers after they are sent
  • How to synchronize jQuery dialog box to act like alert() of Javascript
  • calculate gradient output for Theta update rule
  • MongoDb aggregation
  • OpenGL 3.3 on Mac OSX El Capitan with LWJGL
  • Update CALayer sublayers immediately
  • How can I remove ASP.NET Designer.cs files?
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?