Re: [RFC] Generalize support of negative string offsets
| From: | Stanislav Malyshev | Date: | Sat, 06 Feb 2016 21:39:27 +0000 |
| Subject: | Re: [RFC] Generalize support of negative string offsets | ||
| References: | 1 2 3 4 5 6 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-91102@lists.php.net to get a copy of this message | ||
Hi!
> if it means one has to take a brief look in the manual. String offset is
> *not* the same as array access.
>
>
> What are these subtle differences?
Negative indexes, string indexes. Array offset is a variable, so it can
be used by-ref, but string offset can not. You can substitute object for
array access, and it would work, but not for string offset. You can do
$foo[1]++ for arrays, but not for string offsets, same with assign-ops.
$foo[1]="bar" works differently - for string offset, it will take only
one letter. $foo[1][1] works for arrays but not string offsets. Same for
[]. $foo[1] = 1 would assign integer for array, but string "1" for strings.
It goes deeper. For arrays, $foo[1] is a regular variable, with pretty
much everything working on it that works on $foo. For strings, $foo[1]
is a very special thing called "string offset", and there are a lot of
things that won't work with it. So if you apply your intuition, it'd
serve you fine with arrays (can I do ++ on $foo[1]? Sure, why not - it's
just a variable). But for string offsets, you have to develop completely
different set of rules, as it is a special entity to which regular
variable rules do not apply.
> I agree that different things should use different syntax. However, the
> point here is that all of array access, ArrayAccess and string offset
> access are really the same thing and as such should also use the same
I think I demonstrated that ArrayAccess and offsets are not the same
thing. At least not currently.
--
Stas Malyshev
smalyshev@gmail.com