|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2009-09-05 10:08 UTC] jani@php.net
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Dec 07 02:00:01 2025 UTC |
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