Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/277.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 如何创建一个动态$this调用?_Php - Fatal编程技术网

Php 如何创建一个动态$this调用?

Php 如何创建一个动态$this调用?,php,Php,Eddited: 我有课。 I类cunstructor I从表中检索所有行数据 像这样: $this->isalonefile = $row['isalonefile']; $this->tzfile = $row['tzfile']; 该表有两列:tzfile和isalonefile 在其中一种方法中,我希望在上述两个值之间动态选择 例如(这是非常简化的…): 我想创建一个动态方法,如 public function foo($var){

Eddited: 我有课。 I类cunstructor I从表中检索所有行数据 像这样:

$this->isalonefile = $row['isalonefile'];
$this->tzfile = $row['tzfile'];
该表有两列:
tzfile
isalonefile
在其中一种方法中,我希望在上述两个值之间动态选择

例如(这是非常简化的…):

我想创建一个动态方法,如

      public function foo($var){

              $val = $this->$var;

   }

如何操作?

如果$this是您的类实例,则只能访问该类实例的方法和属性。我认为为现有属性获取新变量没有意义。这将使您的代码无法解码


最好直接访问属性和方法。

这与PDO有什么关系?请澄清您的问题,以便我们知道1.)您期望它做什么,2.)它实际做什么,以及3.)您迄今为止为解决问题所做的努力。此外,请提供一份声明,说明你试图做的是一种极其有害的行为。别这么做,我给PDO贴标签了吗?所以这可能是一个错误。我将编辑这个问题。如果您谈论的是变量(),请尝试
$var=“file”。但这通常是一些设计问题的迹象,在很多情况下,阵列可以提供更好的解决方案。您的代码有什么问题?看起来它已经可以做你想做的了
      public function foo($var){

              $val = $this->$var;

   }