Adding to John Crenshaw's list of reasons not to implicitly
treat strings as arrays in foreach loops... Please keep in
mind the following valid code:
$s = 'hello';
foreach ((array)$s as $x) {
var_dump($x);
}
The result is:
string(5) "hello"
That behavior can be handy. Hopefully, a BC break wouldn't
occur if any of the string features currently being discussed
are implemented. Without a BC break, having strings implicitly
be treated as arrays in foreach loops will seem very strange
in cases like the above.
Iterators are nice. Having a "text_string_to_array" function
would also be fine. For example:
$s = 'hello';
foreach (text_string_to_array($s) as $x) {
var_dump($x);
}
The result would be:
string(1) "h"
string(1) "e"
string(1) "l"
string(1) "l"
string(1) "o"
I don't know enough about the internals to say for sure, but
perhaps text_string_to_array() could be implemented as creating
a reference to the string that has a flag set that causes
it to be allowed to be treated as an array. (A full conversion
might be needed it were written to. For example,
$a = text_string_to_array($s); $a[0] = 5; )
- Todd
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php