On 24/11/11 12:44, Ferenc Kovacs wrote:
>
>
> On Thu, Nov 24, 2011 at 1:38 AM, David Muir <davidkm...@gmail.com
> <mailto:davidkm...@gmail.com>> wrote:
>
>     Just to clarify, the changes introduced in 5.4 will result in the
>     following:
>
>     <?php
>
>     $string = 'foo';
>     $array = array(
>        'foo' => array(
>            'bar' => 'baz',
>            //expected structure
>            //'bar' => array('baz' => array('values'))
>     ));
>
>     var_dump(
>        isset($string['foo']), //true
>        isset($string[0][0]), //false, true in 5.4
>        isset($array['foo']['bar'][0]), //true
>        isset($array['foo']['bar']['baz']), //true
>        isset($array['foo']['bar']['baz']['0']) //false, true as of 5.4
>        isset($string['foo']['bar']['baz']['0']) //false, true as of 5.4
>     );
>
>
> you are missing a comma from the end of the
> isset($array['foo']['bar']['baz']['0']) //false, true as of 5.4
> line

Yeah, I added that one at the last minute. That's what I get for a quick
copy/paste...

>
> isset($string['foo']['bar']['baz']['0']) //false, true as of 5.4
> gives me a fatal error on 5.3 ("PHP Fatal error:  Cannot use string
> offset as an array" as you can't "chain" string offsets before 5.4)
>

It gives me false in 5.3.6. Using it outside of isset() results in the
fatal error.

>  
>
>
>     What used to be a one-liner, now effectively needs a crapload of
>     boilerplate code.
>
>
> yeah, if you can't trust the data structures in your code, then you
> have to validate it.
> don't forget that this bug could also bite you with 5.3:

True, it does fail in 5.3 in some places, but it now fails in more
places with 5.4. Maybe what's triggering the hostile reaction to this
change is that people incorrectly assumed that they were validating it
in a valid manner. The docs for isset states that it works for array
keys, but there's no mention about the string offset gotcha, and it's a
much more convenient option that using array_key_exists + is_array for
each level.

Can we add the lines:

var_dump(isset($a['pie']['a'][0]));        // TRUE
var_dump(isset($a['pie']['a']['b']));        // TRUE

to the isset docs? At least then the current behaviour would be
documented. And maybe add Anthony Ferrera's tip that only the last
element needs an array check?


> Personally I think that the main issue is that we silently convert
> $foo['bar'] to $foo[0] for strings, which imo rarely the intended
> behavior and doesn't really makes much sense (except that the offset
> expects the index to be int, and this is how we type juggle between
> string and int), so I think it would be a good idea to raise a notice
> here, so the developer can fix his code, or add some input validation.

That would be a help.


Cheers,
David

Reply via email to