Req #52381 [Opn->Bgs]: You're doing the wrong thing with string accessors
| From: | aharvey@php.net | Date: | Tue, 20 Jul 2010 04:08:47 +0000 |
| Subject: | Req #52381 [Opn->Bgs]: You're doing the wrong thing with string accessors | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-152539@lists.php.net to get a copy of this message | ||
Edit report at http://bugs.php.net/bug.php?id=52381&edit=1
ID: 52381
Updated by: aharvey@php.net
Reported by: mbt at gator dot net
Summary: You're doing the wrong thing with string accessors
-Status: Open
+Status: Bogus
Type: Feature/Change Request
Package: Unknown/Other Function
Operating System: Linux
PHP Version: 5.3.2
New Comment:
This decision dates back the best part of five years at this stage to
the developer meeting in November 2005. [0] It's come up a couple of
times on the Internals mailing list since then -- both immediately
after the developer meeting [1], and then to confirm the 5.3 migration
document was correct. [2]
To be blunt, the horse has already bolted on this. {} is deprecated,
and [] is the supported way to access strings by offset both now and
in the future.
[0] http://www.php.net/~derick/meeting-notes.html#cleanup-for-vs
[1] http://www.mail-archive.com/internals@lists.php.net/msg18638.html
[2] http://www.mail-archive.com/internals@lists.php.net/msg42883.html
Previous Comments:
------------------------------------------------------------------------
[2010-07-20 01:53:12] mbt at gator dot net
Description:
------------
I should have made more fuss about this earlier, but here it is now.
I think you are taking exactly the wrong approach to addressing single
characters in strings. I am of the very strong opinion that the direct
addressing of characters in strings have an obvious syntactic difference
from
the addressing of array elements. This means you should keep {} and
deprecate
[] for addressing characters in strings.
PHP is not C!! In C, strings are arrays, so [] is the preferred C
operator to
address single bytes (or character codes) in a string. In PHP, strings
are a
primitive type; they are not arrays. The notation $foo{0} makes it
plain that
$foo is a string, not an array and that you are accessing a byte, not an
array
element. Making this obvious through the syntax is more important than
you
might think.
Imagine $bar to be a 3x3 array of strings. In this case, what are you
to
assume when you see in the middle of a script, far away from the
assignment to
$bar, when you read
$baz = $bar[$a][$b][$c];
If you have instead
$baz = $bar[$a][$b]{$c};
you know you're not doing ordinary array addressing.
These issues become important for the people who have to maintain the
code.
Consider two more ways confusion can arise because strings are not
arrays.
If $abc is a string,
$d = $abc[2];
works but
$abc[2] = array('boo');
fails. The elements of a string "array" do not work like true array
elements.
Indices to strings must be integers; unlike arrays, strings do not
allow
associative indices. This fails:
$abc['key'] = 'def';
I believe that indicating single-byte access via [] is such a bad idea
that if
you're dead set on removing {}, I would prefer to deprecate that kind of
access
altogether in favor of substr() and friends.
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/bug.php?id=52381&edit=1