Accessing object's method within string

Posted by Wild One on Stack Overflow See other posts from Stack Overflow or by Wild One
Published on 2012-10-20T22:53:37Z Indexed on 2012/10/20 23:00 UTC
Read the original article Hit count: 115

Filed under:
|

Recently I was reading php documentation and found interesting note in string section:

Functions, method calls, static class variables, and class constants inside {$} work since PHP 5. However, the value accessed will be interpreted as the name of a variable in the scope in which the string is defined. Using single curly braces ({}) will not work for accessing the return values of functions or methods or the values of class constants or static class variables.

See www.php.net/manual/en/language.types.string.php

It says, that I can't use curly syntax to get value returned by object's method call. Is it a mistake in manual or I misunderstood it, because I tried the following code and it works just fine:

<?php
class HelloWorld
{
    public static function hello() 
    {
        echo 'hello';
    }
}
$a = new HelloWorld();

echo "{$a->hello()} world";

© Stack Overflow or respective owner

Related posts about php

Related posts about string