Re: foreach() for strings
| From: | Derick Rethans | Date: | Mon, 20 Jun 2011 12:07:48 +0000 |
| Subject: | Re: foreach() for strings | ||
| References: | 1 2 3 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-53394@lists.php.net to get a copy of this message | ||
On Mon, 20 Jun 2011, Robert Eisele wrote:
> 2011/6/20 Derick Rethans <derick@php.net>
>
> > On Mon, 20 Jun 2011, Robert Eisele wrote:
> >
> > > foreach() has many functions, looping over arrays, objects and
> > > implementing the iterator interface. I think it's also quite
> > > intuitive to use foreach() for strings, too.
> >
> > > If you want to implement a parser in PHP, you have to go the way
> > > with for + strlen + substr() or $x[$i] to address one character of
> > > the string.
> >
> > Yes, this sounds like a good addition to me. One question though,
> > what to do with an object that implements __toString() ?
> >
>
> That's the question, maybe one must force __toString() via an explicit
> string-cast:
>
> foreach( (string) $obj as $k=>$v)
That's a sensible thing indeed. We just need to make sure we document
this with an example and a test then.
cheers,
Derick
--
http://derickrethans.nl | http://xdebug.org
Like Xdebug? Consider a donation: http://xdebug.org/donate.php
twitter: @derickr and @xdebug