Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/237.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP:dynamic$this->;类别->;功能_Php_Oop - Fatal编程技术网

PHP:dynamic$this->;类别->;功能

PHP:dynamic$this->;类别->;功能,php,oop,Php,Oop,我想知道是否有像动态类和函数调用这样的东西是可能的 例如,我有两个操作: $this->foo->value(); $this->bar->value(); 我想这样称呼它: $this->($var)->value(); 现在我正在这样做(但是我喜欢上面的方法) 你差点就成功了 $var = 'foo'; $this->$var->value(); 相当于 $this->foo->value(): 同样地: $var = '

我想知道是否有像动态类和函数调用这样的东西是可能的

例如,我有两个操作:

$this->foo->value();

$this->bar->value();
我想这样称呼它:

$this->($var)->value();
现在我正在这样做(但是我喜欢上面的方法)

你差点就成功了

$var = 'foo';
$this->$var->value();
相当于

$this->foo->value():
同样地:

$var = 'bar';
$this->foo->$var();
相当于

$this->foo->bar();

你在花括号上的位置是什么<代码>$this->{$var}->value()?我倾向于喜欢它们,因为它们使方法是一个变量这一点在视觉上更加清晰。很酷,谢谢,这确实有效。。您还知道这样的事情是否可能:$this->($this->pageClass->value)->value()?哇,谢谢你。。这就成功了:D我想投票给你们两个,但遗憾的是,作为新成员,我还不能PHP是解释的,因此您可以使用对象而无需首先定义它们。
$this->foo->bar();