New PHP feature, comments please!

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
crazycoders
Forum Contributor
Posts: 260
Joined: Tue Oct 28, 2008 7:48 am
Location: Montreal, Qc, Canada

New PHP feature, comments please!

Post by crazycoders »

Hi, i'd like to suggest a few new magic methods regarding foreach or the domain of iteration. I don'T know where to post that anyway in the WWW so i'll post it here and see if anyone knows where to go next.

Methods:
__enum : Occurs when an object must be enumerated. When calling this function, it should return false if there are no array representation possible or return an array of elements to iterate
__next : In the event __enum failed, did not exist or returned false, __next is used to tell the object to push the internal enumeration pointer to the next item and returns true if an object exists, false if EOF
__current: Again, just like __next, this returns the current object from the enumeration.

So, in a Foreach, the magic methods would be called in this order

foreach($object as $value)
-> $object::__enum();

if __enum returned anything that is not enumerable (not an object or array) then foreach keeps $object as the enumeration source and loops:

1. __current: return the current pointer and foreach stores it as $value
2. __next: returns true if more items exist, false if nothing left
3. start over at __current until __next returns false;

Would this be an interresting feature?

I see a use for this because i do a lot of OOP in PHP and am forced to create a method that dumps my objects to an array manually when i want to iterate them such as:

foreach($object->dump() as $value){
...
}

Instead, if a magic method existed, it would be more logical and natural to implement enumerations this way.

Thanks
Math
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: New PHP feature, comments please!

Post by s.dot »

Something like object iterators ?
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
allspiritseve
DevNet Resident
Posts: 1174
Joined: Thu Mar 06, 2008 8:23 am
Location: Ann Arbor, MI (USA)

Re: New PHP feature, comments please!

Post by allspiritseve »

scottayy wrote:Something like object iterators ?
I second that. Use the SPL Iterator interface.
crazycoders
Forum Contributor
Posts: 260
Joined: Tue Oct 28, 2008 7:48 am
Location: Montreal, Qc, Canada

Re: New PHP feature, comments please!

Post by crazycoders »

Oh well, seems i didn't look long enough...
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: New PHP feature, comments please!

Post by josh »

the PHP documentation for the SPL sucks so I'm posting this
http://www.phpro.org/tutorials/Introduction-to-SPL.html
crazycoders
Forum Contributor
Posts: 260
Joined: Tue Oct 28, 2008 7:48 am
Location: Montreal, Qc, Canada

Re: New PHP feature, comments please!

Post by crazycoders »

Thanks i'll look at that...
Post Reply