Req #34341 [Opn->Wfx]: No warning for $a['zz'] if $a is not an assoc array
| From: | yohgaki@php.net | Date: | Wed, 31 Jul 2013 03:15:38 +0000 |
| Subject: | Req #34341 [Opn->Wfx]: No warning for $a['zz'] if $a is not an assoc array | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-180602@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=34341&edit=1
ID: 34341
Updated by: yohgaki@php.net
Reported by: herve dot pages at laposte dot net
Summary: No warning for $a['zz'] if $a is not an assoc array
-Status: Open
+Status: Wont fix
Type: Feature/Change Request
-Package: Feature/Change Request
+Package: *General Issues
Operating System: Fedorac Core 3
PHP Version: 4.4.0
Block user comment: N
Private report: N
New Comment:
String offset access is defined now, but
PHP does not raise errors for scalar types. (e.g. integer, bool)
Raising errors for this kind of access will break many codes. It's unlikely this
behavior will be changed.
Previous Comments:
------------------------------------------------------------------------
[2005-09-02 03:44:54] herve dot pages at laposte dot net
Description:
------------
PHP doesn't output any warning for $a['zz'] if $a is not an assoc array.
This problem has been reported in Bug #33879 in the particular case where $a is a string. Since
individual chars in a string can be accessed using the $a[] syntax, and since the key 'zz'
will be automatically cast to 0, then I can understand why PHP will accept $a['zz']
without complaining.
But when $a is an integer, or a bool, or anything else that has nothing to do with an array (or a
string), I don't see any reason why PHP should accept $a['zz'] without complaining!
It would make some bugs much easier to find...
Reproduce code:
---------------
<?php
error_reporting(E_ALL);
$assoc = array();
$test = $assoc['zz']; // Warning "Undefined index: zz..." issued. OK
// Now what if the $assoc variable is replaced by a variable that is not
// an assoc array?
// The expected/natural behaviour would be that PHP complains even
// stronger because if such situation were occuring with a compiled
// language, any descent compiler would not even compile.
$not_an_assoc = 85;
$test = $not_an_assoc['zz']; // No warning issued!
echo gettype($test); // -> Display NULL
$not_an_assoc = "85";
$test = $not_an_assoc['zz']; // No warning issued!
echo $test; // -> Display 8
?>
Expected result:
----------------
It would be nice if PHP could tell me I'm doing something wrong with a warning or an error
(that's why I'm using error_reporting(E_ALL) after all).
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=34341&edit=1