#49471 [Opn->Bgs]: Multiple object selection operators in quoted strings
ID: 49471
Updated by: jani@php.net
Reported By: tudor at tudorholton dot com
-Status: Open
+Status: Bogus
Bug Type: Scripting Engine problem
Operating System: Ubuntu
-PHP Version: 5.2SVN-2009-09-05 (snap)
+PHP Version: 5.2.6
New Comment:
RTFM: http://www.php.net/types.string#language.types.string.parsing
This works fine:
echo "{$b->a->attr}";
Previous Comments:
------------------------------------------------------------------------
[2009-09-05 05:41:54] tudor at tudorholton dot com
Description:
------------
This is actually on Ubuntu Jaunty which is PHP version
5.2.6-3ubuntu4.2
Using multiple object access operators in a row inside a double-quoted
string causes the error:
Catchable fatal error: Object of class ... could not be converted to
string
The problem is that the operators are interpreted from left to right
and then converted to string. The last operation should be that the
object is converted to a string.
This is particularly important when using OO code because frequently
the current object ($this) references another object and then gets an
attribute from that. e.g. $this->that->attribute
Reproduce code:
---------------
<?php
class A { var $attr = 'Hello A!'; }
class B {
var $a;
var $attr = "Hello B!";
function __construct() {
$this->a = new A();
}
function output() {
echo "$this->attr";
}
}
$b = new B();
$b->output();
echo "$b->attr";
echo "$b->a->attr";
?>
Expected result:
----------------
Hello B!Hello B!Hello A!
Actual result:
--------------
Hello B!Hello B!
Catchable fatal error: Object of class A could not be converted to
string in test.php on line 19
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=49471&edit=1
Thread (2 messages)