php - 如何在 PHP 中使大的多维变量看起来更短?

标签 php variables scope

我正在创建一个 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/

相关文章:

javascript - 在原型(prototype)方法中修改内部函数的实例变量

Javascript/AJAX、PHP 和 MySQL 结账

python - 不要每次迭代都检查变量

ruby-on-rails - rails : Scoped ID in addition to primary id

ruby-on-rails - 在 rails 3 中查找没有关联记录的记录

mysql - 在 "INSERT ON"子句中使用用户变量

php - 用于匹配空格或字符串结尾的正则表达式

php - 决策树 - 一定是更好的方法......?

javascript - 我的 WordPress 网站上的 jQuery 脚本均无法正常工作

C - 将新数据附加到链表也会更改以前的数据