Edit report at https://bugs.php.net/bug.php?id=63092&edit=1

 ID:                 63092
 Comment by:         mail+php at requinix dot net
 Reported by:        slogger at lavabit dot com
 Summary:            IteratorAggregate interface should allow for
                     getIterator to be static
 Status:             Open
 Type:               Bug
 Package:            Class/Object related
 Operating System:   All
 PHP Version:        Irrelevant
 Block user comment: N
 Private report:     N

 New Comment:

>If following common architectures/design patterns, the following case is quite
>common:
Not that I've ever seen. You're essentially asking for a built-in registry 
pattern where one doesn't make sense. There are just too many problems trying 
to 
do it that way.

This isn't the place for discussion, but if you want magic Iterator behavior 
then 
a factory would be a better option.

$factory = new ThingFactory(); // implements IteratorAggregate
$dog = $factory->create("dog"); // create() adds the object to $this->things
$cat = $factory->create("cat");

foreach($factory as $thing) {
    echo "I was used to create a {$thing->name}\n";
}


Previous Comments:
------------------------------------------------------------------------
[2012-09-14 21:37:59] slogger at lavabit dot com

Description:
------------
See:
http://www.php.net/manual/en/class.iteratoraggregate.php

If following common architectures/design patterns, the following case is quite 
common:

// Given a class Thing where the constructor sets a 'name' property
// and implements IteratorAggregate:
dog = new Thing('dog');
cat = new Thing('cat');
$itty = Thing::getIterator();
foreach($itty as $thing){
  echo "{$thing->name} \n";
}
// dog
// cat

However, the current IteratorAggregate does not allow for getIterator() to be 
static. This seems like a big flaw. Obviously nothing prevents creation of such 
a static method and NOT implementing IteratorAggregate, but that seems kind of 
wrong.

Obviously the issue of how getIterator should work if it can be either static 
or non-static is kind of fugly, but I tend to think that it ought to have been 
static to begin with -- usually you want to iterate types/collections, not an 
individual thing/instance.

Test script:
---------------
// Obviously this is a crude example, but it shows the error.
// In a more complex example, $things might be some sort of Iterator, rather
// than just an array.

class Thing implements IteratorAggregate{
   public static $things = array();
   public function __construct($name){
      $this->name = $name;
      static::$things[] = $this;
   }
   public static function getIterator(){
      return static::$things;
   }
}

$dog = new Thing('dog');

foreach(Thing::getIterator() as $thing){
   echo $thing->name;
}

Expected result:
----------------
dog

Actual result:
--------------
Fatal error: Cannot make non static method IteratorAggregate::getIterator() 
static in class Thing in php shell code on line 1


------------------------------------------------------------------------



-- 
Edit this bug report at https://bugs.php.net/bug.php?id=63092&edit=1

Reply via email to