From: slogger at lavabit dot com Operating system: All PHP version: Irrelevant Package: Class/Object related Bug Type: Bug Bug description:IteratorAggregate interface should allow for getIterator to be static
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 bug report at https://bugs.php.net/bug.php?id=63092&edit=1 -- Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=63092&r=trysnapshot54 Try a snapshot (PHP 5.3): https://bugs.php.net/fix.php?id=63092&r=trysnapshot53 Try a snapshot (trunk): https://bugs.php.net/fix.php?id=63092&r=trysnapshottrunk Fixed in SVN: https://bugs.php.net/fix.php?id=63092&r=fixed Fixed in SVN and need be documented: https://bugs.php.net/fix.php?id=63092&r=needdocs Fixed in release: https://bugs.php.net/fix.php?id=63092&r=alreadyfixed Need backtrace: https://bugs.php.net/fix.php?id=63092&r=needtrace Need Reproduce Script: https://bugs.php.net/fix.php?id=63092&r=needscript Try newer version: https://bugs.php.net/fix.php?id=63092&r=oldversion Not developer issue: https://bugs.php.net/fix.php?id=63092&r=support Expected behavior: https://bugs.php.net/fix.php?id=63092&r=notwrong Not enough info: https://bugs.php.net/fix.php?id=63092&r=notenoughinfo Submitted twice: https://bugs.php.net/fix.php?id=63092&r=submittedtwice register_globals: https://bugs.php.net/fix.php?id=63092&r=globals PHP 4 support discontinued: https://bugs.php.net/fix.php?id=63092&r=php4 Daylight Savings: https://bugs.php.net/fix.php?id=63092&r=dst IIS Stability: https://bugs.php.net/fix.php?id=63092&r=isapi Install GNU Sed: https://bugs.php.net/fix.php?id=63092&r=gnused Floating point limitations: https://bugs.php.net/fix.php?id=63092&r=float No Zend Extensions: https://bugs.php.net/fix.php?id=63092&r=nozend MySQL Configuration Error: https://bugs.php.net/fix.php?id=63092&r=mysqlcfg