On Wed, Nov 23, 2011 at 7:52 PM, Stas Malyshev <smalys...@sugarcrm.com>wrote:

> Hi!
>
>
> On 11/23/11 6:14 AM, Daniel Convissor wrote:
>
>> To me, this is the bug.  $a['blah'] does not exist.  An undefined index
>>
>
> If it's a bug, this bug was in PHP since forever, nothing new here.
>
>
>  notice should be raised.  The key "blah" should not be converted to 0.
>> The following two things should behave the same:
>>
>> $b = array('exists' =>  'foo');
>> echo $b['blah'] . "\n";
>>
>> $a = 'foo';
>> echo $a['blah'] . "\n";
>>
>
> No they should not. First is array access, second is string offset access,
> totally different ops. That's like saying + on arrays should calculate the
> sum of all array elements because + on numbers calculates the sum.
> Operators have different meanings for different types, it has always been
> so in all languages.
> One could argue that string access with non-string should produce a
> notice, but probably if we added that a lot of people would come out to
> complain we broke their perfectly working code ;)


I think that it would make sense.
I mean using $foo['asd'] where $foo is a string is as likely to be a hidden
application bug as the array() => 'Array' conversion that we patched
recently to trigger a warning.

More clarification for the others who seems to be miss how specific this
change is:
The only case where the 5.4 branch works differently as before if you
reference a string type(int, float, etc. won't trigger this) variable using
an associative index and you expect it that to be undefined variable even
though that the documentation explicitly states that the index will be
converted to an integer in this case.
for example:

$foo = 'string';
echo isset($foo['bar']);

will return true (as $foo['bar'] will converted to $foo[0] which returns
's' in this case) in 5.4 while it returned false previously.

ps:
on a sidenote: the same change also introduces that using the following:
$foo[0][0][0]... on a non-empty string will be always valid, before this
change it produced a "PHP Fatal error:  Cannot use string offset as an
array"

-- 
Ferenc Kovács
@Tyr43l - http://tyrael.hu

Reply via email to