php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #49471 Multiple object selection operators in quoted strings
Submitted: 2009-09-05 05:41 UTC Modified: 2009-09-05 10:08 UTC
From: tudor at tudorholton dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.2.6 OS: Ubuntu
Private report: No CVE-ID: None
 [2009-09-05 05:41 UTC] 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

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-09-05 10:08 UTC] jani@php.net
RTFM: http://www.php.net/types.string#language.types.string.parsing

This works fine:

  echo "{$b->a->attr}";


 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sun Dec 07 01:00:01 2025 UTC