使用属性值methodname php按名称调用方法
首先,这里是一个代码示例,它可以工作:使用属性值methodname php按名称调用方法,php,syntax,Php,Syntax,首先,这里是一个代码示例,它可以工作: <?php class Foo { private $name = "test"; public function __construct() { $name = $this->name; $this->$name(); } function test() { echo "test"; } } $foo = new Foo(); ?
<?php
class Foo
{
private $name = "test";
public function __construct()
{
$name = $this->name;
$this->$name();
}
function test()
{
echo "test";
}
}
$foo = new Foo();
?>
现在,我的问题是:是否可以直接使用类Foo的artAttribute名称来调用方法测试,而不创建如下新变量:
<?php
class Foo
{
private $name = "test";
public function __construct()
{
$this->$name();
}
function test()
{
echo "test";
}
}
$foo = new Foo();
?>
期待您的回答,
Heeiigou像这样用{}把它包起来:
function __construct() {
$this->{$this->name}();
}
嗯,为什么不在构造函数中调用
$this->test()
?无论如何,$this->{$this->name}()代码>应该做得很好,Thx这个工作很好。你能告诉我你的背景吗?为什么这样做?看看细节