这是一个有效的类变量吗? $这 - > $ VARIABLENAME

时间:2017-12-08 14:19:23

标签: php class this

我正在引用像这样的变量

$this->variablename

但我看到像这样访问变量:

$this->$variablename

例如在Google脚本中

public function __get($key)
{
    $keyTypeName = $this->keyType($key);
    $keyDataType = $this->dataType($key);
    if (isset($this->$keyTypeName) && !isset($this->processed[$key])) {
      if (isset($this->modelData[$key])) {
        $val = $this->modelData[$key];
      } else if (isset($this->$keyDataType) &&
          ($this->$keyDataType == 'array' || $this->$keyDataType == 'map')) {
        $val = array();
      } else {
        $val = null;
      }
   }
}

在此示例中为$this->$keyTypeName,这是错误还是有效代码?在PHP 7中它给出了错误。

3 个答案:

答案 0 :(得分:2)

是的,他们被称为variable variables。它访问data1['new'] = data1['target'].map(s) print (data1.head()) target sepal length (cm) sepal width (cm) petal length (cm) \ 0 0.0 5.1 3.5 1.4 1 0.0 4.9 3.0 1.4 2 0.0 4.7 3.2 1.3 3 0.0 4.6 3.1 1.5 4 0.0 5.0 3.6 1.4 petal width (cm) new 0 0.2 238.1 1 0.2 238.1 2 0.2 238.1 3 0.2 238.1 4 0.2 238.1 print (data1.tail()) target sepal length (cm) sepal width (cm) petal length (cm) \ 145 2.0 6.7 3.0 5.2 146 2.0 6.3 2.5 5.0 147 2.0 6.5 3.0 5.2 148 2.0 6.2 3.4 5.4 149 2.0 5.9 3.0 5.1 petal width (cm) new 145 2.3 149.0 146 1.9 149.0 147 2.0 149.0 148 2.3 149.0 149 1.8 149.0

答案 1 :(得分:0)

它们是变量的变量

$this->$keyTypeName
This is accessed as $$keyTypeName

,而

$this->keyTypeName
this is accessed as $keyTypeName

答案 2 :(得分:0)

See Example

什么是$ this-> $ var simple意味着php解释器转换$ var并将其与对象$ this一起解析。

如果$ var =" string&#34 ;;

你说$ this-> $ var,它只是意味着$ this-> string。

检查附带的示例链接。