PHP - 使用字符串""前缀中的类函数不起作用

Nim*_*007 -1 php web

试图在echo中的字符串中使用我的类的函数是不行的,可能是因为字符串"",有更好的方法吗?

这是我的代码:

class example{
    private $name = "Cool";

    function getName(){
        return $this->name;
    }
}


$example = new example();

//THIS WONT WORK 
echo "the name : $example->getName()";
//THIS WILL PRINT :
//the name : ()

//THIS WILL WORK
$name = $example->getName();
echo "the name : $name";
//THIS WILL PRINT :
//the name : Cool
Run Code Online (Sandbox Code Playgroud)

怎么能在字符串里面实现呢?

谢谢

Yog*_*har 5

{}在双引号内调用类函数时必须使用.

echo "the name : {$example->getName()}";
Run Code Online (Sandbox Code Playgroud)