Results 1 to 3 of 3

Thread: 1D object in string works, 2D object doesn't - puzzled

  1. #1
    Join Date
    Jan 2006
    South Africa

    1D object in string works, 2D object doesn't - puzzled

    Hi, everyone!

    I have come across a "feature" in PHP5 that is not a great problem to me, but has me puzzled. Can anyone shine some light on this?

    The following snippet works fine:
    $x->y = "hello";
    echo "Let me say $x->y to you!\n";

    However, if I modify $x->y (what I shall loosely call a one-dimensional object, analogous to arrays) to $x->y->z (a "two-dimensional object" for lack of a better term) as follows:
    $x->y->z = "hello";
    echo "Let me say $x->y->z to you!\n";

    ...this snippet generates the following error message:
    Catchable fatal error: Object of class stdClass could not be converted to string

    When I change it to:
    $x->y->z = "hello";
    echo "Let me say " . $x->y->z . " to you!\n";

    ... then all is well again. So that's the work-around, problem solved, fine... but what has me puzzled as to why this behavior occurs in the first place.

    Ideas, anyone?

    // FvW

  2. #2
    Join Date
    Aug 2004
    I think it's a question of how the parser identifies variables to be interpolated within a double-quoted string, as "complex" variable notation works, making it clear to the parser where the variable begins/ends within the string:
    PHP Code:
    "Let me say {$x->y->z} to you!\n"
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  3. #3
    Join Date
    Jan 2006
    South Africa
    Yep, that works! And it's easier than concatenating strings with dots. :-)

    There are obviously limitations to the parser's ability to recognize variables embedded in strings... but if these are well documented I haven't been able to find out much about it. Oh well. Onward! :-)


    // FvW

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center