Hi all,
I came across with bug #68947 https://bugs.php.net/bug.php?id=68947
and realized small inconsistency.
$obj->${array[$key]}; // Syntax error
$obj->{$array[$key]}; // Works
$obj->${key}; // E_NOTICE. Does not work
$obj->{$key}; // Works
echo "${array[$key]}"; // Works
echo "{$array[$key]}"; // Works
Of course, script/string parsing aren't the same. Are there technical
reasons
why ${value} is not allowed? If there are, we may encourage
echo "{$array[$key]}"; // Works
rather than
echo "${array[$key]}"; // Works
Regards,
--
Yasuo Ohgaki
yohgaki@ohgaki.net
Hi,
Hi all,
I came across with bug #68947 https://bugs.php.net/bug.php?id=68947
and realized small inconsistency.$obj->${array[$key]}; // Syntax error
$obj->{$array[$key]}; // Works$obj->${key}; // E_NOTICE. Does not work
$obj->{$key}; // Worksecho "${array[$key]}"; // Works
echo "{$array[$key]}"; // WorksOf course, script/string parsing aren't the same. Are there technical
reasons
why ${value} is not allowed? If there are, we may encourageecho "{$array[$key]}"; // Works
rather than
echo "${array[$key]}"; // Works
Is somebody actually recommending the latter form? I'm surprised that
it even works ... that should be a syntax error IMO.
Cheers,
Andrey.
Hi Andrey,
Is somebody actually recommending the latter form? I'm surprised that
it even works ... that should be a syntax error IMO.
Do you mean
echo "${array[$key]}"; // Works
should be errror?
This form is common in shell scripting.
http://tldp.org/LDP/abs/html/parameter-substitution.html
I'm using it regularly. I guess many users expect following forms to work.
$obj->${array[$key]}; // Syntax error
$obj->${key}; // E_NOTICE. Does not work
What others expect?
Regards,
--
Yasuo Ohgaki
yohgaki@ohgaki.net
Hi Yasuo,
Hi Andrey,
Is somebody actually recommending the latter form? I'm surprised that
it even works ... that should be a syntax error IMO.Do you mean
echo "${array[$key]}"; // Works
should be errror?
This form is common in shell scripting.http://tldp.org/LDP/abs/html/parameter-substitution.html
I'm using it regularly. I guess many users expect following forms to work.
$obj->${array[$key]}; // Syntax error
$obj->${key}; // E_NOTICE. Does not workWhat others expect?
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.
Andrea Faulds
http://ajf.me/
Hi Andrea,
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.
Andrey, the reason why you are surprised is probably because of this
// echo "${obj->var}"; // Syntax error
Regards,
--
Yasuo Ohgaki
yohgaki@ohgaki.net
Hi,
Hi Andrea,
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.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.
Hi all,
I came across with bug #68947 https://bugs.php.net/bug.php?id=68947
and realized small inconsistency.$obj->${array[$key]}; // Syntax error
$obj->{$array[$key]}; // Works$obj->${key}; // E_NOTICE. Does not work
$obj->{$key}; // Worksecho "${array[$key]}"; // Works
echo "{$array[$key]}"; // WorksOf course, script/string parsing aren't the same. Are there technical
reasons
why ${value} is not allowed? If there are, we may encourageecho "{$array[$key]}"; // Works
rather than
echo "${array[$key]}"; // Works
That all seems expected to me. The original point of that bug report is
the BC break due to the uniform variable rfc. The change makes sense,
but just for the record, I have installed about 10 popular, and a couple
of not-so-popular, PHP apps and tested them on PHP 7 over the past week
and this was the only BC break that affected that set of 10 apps.
See:
https://github.com/rlerdorf/avalon/commit/18847d0fe65381977397271c79e04dde72ef86f8
It is listed as a bullet point in a sea of other bullet points in the
UPGRADING file:
. Indirect variable, property and method references are now
interpreted with left-to-right semantics. See details in:
https://wiki.php.net/rfc/uniform_variable_syntax#semantic_differences_in_existing_syntax
But we probably need a section devoted to BC breaks that includes some
real-world code examples.
-Rasmus
Hi all,
Hi all,
I came across with bug #68947 https://bugs.php.net/bug.php?id=68947
and realized small inconsistency.$obj->${array[$key]}; // Syntax error
$obj->{$array[$key]}; // Works$obj->${key}; // E_NOTICE. Does not work
$obj->{$key}; // Worksecho "${array[$key]}"; // Works
echo "{$array[$key]}"; // WorksOf course, script/string parsing aren't the same. Are there technical
reasons
why ${value} is not allowed? If there are, we may encourageecho "{$array[$key]}"; // Works
rather than
echo "${array[$key]}"; // Works
That all seems expected to me. The original point of that bug report is
the BC break due to the uniform variable rfc. The change makes sense,
but just for the record, I have installed about 10 popular, and a couple
of not-so-popular, PHP apps and tested them on PHP 7 over the past week
and this was the only BC break that affected that set of 10 apps.See:
https://github.com/rlerdorf/avalon/commit/18847d0fe65381977397271c79e04dde72ef86f8
It is listed as a bullet point in a sea of other bullet points in the
UPGRADING file:. Indirect variable, property and method references are now
interpreted with left-to-right semantics. See details in:https://wiki.php.net/rfc/uniform_variable_syntax#semantic_differences_in_existing_syntax
But we probably need a section devoted to BC breaks that includes some
real-world code examples.
I agree.
Since ${var} works in quoted string just like shells, there may be
confusion. Extending
http://php.net/manual/en/language.variables.basics.php
or adding new section might be good.
I haven't checked variable variable fully. There may be misbehavior
Apparently, HHVM have missed to support multiple variable variable
reference.
Heads up HHVM folks!
Regards,
--
Yasuo Ohgaki
yohgaki@ohgaki.net