我正在创建一个 php 类,它越深入就越有点失控。
这是一个例子:
unset($this->file[$key]->inspect->formats);
unset($this->file[$key]->inspect->tags);
unset($this->file[$key]->inspect->chapters);
unset($this->file[$key]->inspect->annotations);
unset($this->file[$key]->inspect->automatic_captions);
unset($this->file[$key]->inspect->subtitles);
$this->file[$key]->inspect->name = trim($this->file[$key]->inspect->name);
$this->file[$key]->inspect->artist = trim($this->file[$key]->inspect->artist);
不是为我想使用的每个变量编写 $this->file[$key]->inspect
而是有一种方法可以设置变量,例如 $inspect
占据这个位置?
这样,当我编写 $inspect->subtitles
时,它就会知道我的真正意思并影响主要的 $this->file[$key]->inspect->subtitles
?
最佳答案
$inspect = &$this->file[$key]->inspect;
声明这一点。现在您可以像这样设置数据
$inspect->formats = 'format';
$inspect->subs = 'subs';
// ...
添加 &
您将影响该变量,而不仅仅是该变量的副本
这里是关于引用文献http://php.net/manual/en/language.references.whatare.php的解释
关于php - 如何在 PHP 中使大的多维变量看起来更短?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43980970/