试图在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)
怎么能在字符串里面实现呢?
谢谢
{}在双引号内调用类函数时必须使用.
echo "the name : {$example->getName()}";
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
80 次 |
| 最近记录: |