Re: Use of {} with member.
| From: | Andrey Andreev | Date: | Thu, 29 Jan 2015 23:01:34 +0000 |
| Subject: | Re: Use of {} with member. | ||
| References: | 1 2 3 4 5 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-81393@lists.php.net to get a copy of this message | ||
Hi,
On Fri, Jan 30, 2015 at 12:49 AM, Yasuo Ohgaki <yohgaki@ohgaki.net> wrote:
> Hi Andrea,
>
> On Fri, Jan 30, 2015 at 7:17 AM, Andrea Faulds <ajf@ajf.me> wrote:
>>
>> I would expect that anything within ${} works the same as it does outside
>> it. Having $obj->${array[$key]} do something different to $key =
>> array[$key], $obj->${$key} would make no sense to me.
>
>
> Good point! I forgot about variable variable.
> PHP works as follows now.
>
> http://3v4l.org/dpbES
>
> Andrey, the reason why you are surprised is probably because of this
> // echo "${obj->var}"; // Syntax error
>
Not entirely ... I just have it as a rule of thumb that if it's not
valid code with nothing around it, I shouldn't expect it to be valid
inside curly braces (excluding the semicolon of course). Pretty much
what Andrea said ...
<?php array['foo']; ?> is a syntax error, so I don't expect
${array['foo']} to be valid either. In fact, I just checked and it's
not: http://3v4l.org/B6kdv
echo "${array['foo']}"; probably works only because of how variables
are parsed inside double quotes.
Cheers,
Andrey.