php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #8796 "$foo->bar->baz" ambiguity in double quoted strings
Submitted: 2001-01-19 02:12 UTC Modified: 2002-07-23 21:06 UTC
From: rick at eastcore dot net Assigned:
Status: Wont fix Package: Feature/Change Request
PHP Version: 4.0.4pl1 OS: All
Private report: No CVE-ID: None
 [2001-01-19 02:12 UTC] rick at eastcore dot net
In PHP4 it is possible to construct an object and refer to 
a member using the notation:

$foo->bar

where foo is the object and bar is the member.

There has been some discussion in the php.net documentation
area on OO about the inability to further extend this 
notation to do something like:

print "$foo->bar->baz";

The reason given for not being able to do this is due
to the ambiguity of the multiple -> operators.

Yes, the -> operator could be ambiguous.  The '+', '*', '%', ...  operators could also be ambiguous.  Each language which
uses such binary operators disambiguates them by defining
and associativity and precedence to these operators.

Similarly, for languages which support similar semantics
for an -> operator there is a disambiguating assignment 
of associativity and precedence which allows constructs
like $foo->bar->baz to be unambiguously interpreted.

I am requesting that the specification for PHP be broadened
to include sufficient associativity and precedence constraints on -> to allow $foo->bar->baz to be disambiguated.

I believe the most common disambiguation would have:

$foo->bar->baz

refer to "the value of the member 'baz' of the object denoted by the value of the member 'bar' of the object denoted by the value of the variable foo."

Of course, I forget at the moment whether that's left- or right-associative, and the precedence should presumably be fine set at its current level.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-02-20 01:26 UTC] andre@php.net
there is nothing ambigous (the way you described), except if
enclosed by double quotes in strings, we should maintain
backwards compatibility here, but maybe we shouldn't... :)

I guess (not verified) this applies to here-docs too
 [2001-02-20 08:44 UTC] rick at eastcore dot net
Haven't checked out here-docs either.

On the backwards compatibility issue, I can't see that there would be
(much of) one.  If people aren't using the $foo->bar->baz syntax
within strings then they aren't affected.  If people are using the
syntax it's either not doing what they want/expect or they're plain
weird :-)

Incorporating such an enhancement and saying "$foo->bar->baz in
{strings, here-docs} now does what you'd expect" isn't likely to
cause many ripples (IMHO).


 [2002-07-23 16:28 UTC] jason at mercuryboard dot com
As a workaround for now, just surround it in braces:

print "{$foo->bar->baz}";
 [2002-07-23 21:06 UTC] sniper@php.net
Can't see any bug here or any feature to add or change to be made.. enclosing in {} is the correct way to go..

 [2002-07-23 21:12 UTC] rick at eastcore dot net
Thanks for the speedy resolution!
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Dec 06 02:01:29 2024 UTC