PHP,$ this-> {$ var} - 这是什么意思? [英] PHP, $this->{$var} -- what does that mean?

查看:120
本文介绍了PHP,$ this-> {$ var} - 这是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了访问/更改变量的需要:

I have encountered the need to access/change a variable as such:

$this->{$var}

上下文是使用CI数据图表获取规则。我似乎找不到这个语法实际上做什么。在这种情况下,我们做什么?为什么不只是:

The context is with CI datamapper get rules. I cant seem to find what this syntax actually does. What do the {'s do in this context? Why not just:

$this->var

感谢!

推荐答案

strong>,这样你最终会得到 $ this-> {value-of-$ val}

This is a variable variable, such that you will end up with $this->{value-of-$val}.

请参阅: http://php.net/manual/en/language.variables.variable.php

例如:

$this->a = "hello";
$this->b = "hi";
$this->val = "howdy";

$val = "a";
echo $this->{$val}; // outputs "hello"

$val = "b";
echo $this->{$val}; // outputs "hi"

echo $this->val; // outputs "howdy"

echo $this->{"val"}; // also outputs "howdy"

工作实例: http://3v4l.org/QNds9

这当然是在类上下文中工作。您可以在本地上下文中使用变量变量,就像这样:

This of course is working within a class context. You can use variable variables in a local context just as easily like this:

$a = "hello";
$b = "hi";

$val = "a";
echo $$val; // outputs "hello"

$val = "b";
echo $$val; // outputs "hi"

工作范例:http://3v4l.org/n16sk

这篇关于PHP,$ this-> {$ var} - 这是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆