使用php的对象操作符似乎很奇怪

时间:2014-07-25 15:14:31

标签: php oop

我是PHP的OOP新手。我见过像

这样的代码
<?php
class Response {
public $dict;

public function __construct() {
    $this->dict = new stdClass();
    $this->dict->words = new stdClass();
    $this->dict->words->final = 'test chained string';
}
}

$response = new Response();
$var = 'response->dict->words->final';
$elements = explode('->', $var);
foreach ($elements as $key => $element) {



if ($key == 0) {
  $call = ${$element};
}
 $call = $call->$element;
}

echo $call;

此代码成功打印test chained string

当我在foreach中替换呼叫时,如

foreach ($elements as $key => $element) {


  echo ${$element};

}

我犯了致命错误

我在这里有两个问题:

  1. 实际上$call = $call->$element在这做什么?/
  2. ${$element} ??
  3. 的用法有什么用

0 个答案:

没有答案