Req #33879 [Opn->Csd]: $string["otherstring"] = something | should produce a warning
| From: | requinix@php.net | Date: | Sat, 01 Nov 2014 23:44:49 +0000 |
| Subject: | Req #33879 [Opn->Csd]: $string["otherstring"] = something | should produce a warning | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-188410@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=33879&edit=1
ID: 33879
Updated by: requinix@php.net
Reported by: sigge at hystrix dot se
Summary: $string["otherstring"] = something | should produce
a warning
-Status: Open
+Status: Closed
Type: Feature/Change Request
-Package: Feature/Change Request
+Package: *General Issues
PHP Version: 5.0.4
-Assigned To:
+Assigned To: requinix
Block user comment: N
Private report: N
New Comment:
PHP issues a warning "Illegal string offset '<offset>'" as of 5.4.
http://3v4l.org/dj9pf
Previous Comments:
------------------------------------------------------------------------
[2005-07-27 01:47:39] sigge at hystrix dot se
Description:
------------
I recently had a bug in my code that overwrote an array with a string. I missed the [], it happens.
But debugging was hard, and I had a bunch of $array["foo"] = "bar"; when $array
had been overwritten by a string.
As kind people on #php explained to me, PHP treats any string as 0. While I can understand such
behaviour, I think there should be a warning like "you are treating a string as an array"
or something.
This warning should probably not occur when the key is a number, as this would return the character,
but when accessing a string key of a string makes no sense.
I can't see how this would break any backward compatibility, and it would make debugging much
easier!
Edit: I've now seen that there are similar bugs, all marked closed and without any non-stock
explanation.
I would really like to know why this "is expected", because to me, having developed in PHP
for two years, this wasn't expected at all, and caused me a good headache.
Pardon me if I am spamming, I would prefer to add a comment to those bugs, but can't.
Reproduce code:
---------------
$array = array();
$array = "string";
$array["foo"] = "bar";
Expected result:
----------------
Would be nice if it produced a warning on line 3, "Trying to access a string as an array"
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=33879&edit=1