我是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};
}
我犯了致命错误
我在这里有两个问题:
$call = $call->$element
在这做什么?/ ${$element}
??