PHP 成员变量的大括号语法

发布于 2024-07-27 13:21:30 字数 463 浏览 14 评论 0原文

关于 SO 的第一个问题,它是一个真正的 RTM 候选者。 但我向你保证,我已经看过了,但似乎找不到它。 当我错过了一件简单的事情时,我会很高兴地做一个#headpalm。

试图弄清楚 Zend Framework 并遇到以下语法:

$this->_session->{'user_id'}

我从未见过用于访问看似成员变量的花括号语法。 有何不同

$this->_session->user_id

它与我假设 _session 不相关但将其包含在问题中

,因为它可能不相关。 大括号只是一种简洁约定,试图包装复合变量名 user_id 吗? 或者它是某种特殊的访问器?

任何有关 TFM 的指示以便我可以 R up 将不胜感激。

非常感谢。 请温柔一点。

First question on SO and it's a real RTM candidate. But I promise you I've looked and can't seem to find it. I'll happily do a #headpalm when it turns out to be a simple thing that I missed.

Trying to figure out Zend Framework and came across the following syntax:

$this->_session->{'user_id'}

I have never seen the curly braces syntax used to access what appears to be a member variable. How is it different than

$this->_session->user_id

I'm assuming that the _session is irrelevant, but including it in the question since it may not be.

Are the curly braces just a cleanliness convention that attempts to wrap the compound variable name user_id? Or is it some kind of a special accessor?

Any pointers into TFM so I can R up would be humbly appreciated.

Many thanks. Please be gentle.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

忘东忘西忘不掉你 2024-08-03 13:21:30

大括号用于显式指定变量名的结尾。 例如:

echo "This square is {$square->width}00 centimeters broad."; 

那么,您的案例实际上是两个特殊案例的组合。 您可以使用大括号访问类变量,如下所示:

$class->{'variable_name'} // Same as $class->variable_name
$class->{'variable' . '_name'} // Dynamic values are also allowed

在您的情况下,您只是用大括号语法将它们包围起来。

请参阅 PHP 手册, “复杂(卷曲)语法。”

Curly braces are used to explicitly specify the end of a variable name. For example:

echo "This square is {$square->width}00 centimeters broad."; 

So, your case is really a combination of two special cases. You're allowed to access class variables using curly braces and like so:

$class->{'variable_name'} // Same as $class->variable_name
$class->{'variable' . '_name'} // Dynamic values are also allowed

And in your case, you're just surrounding them with the curly brace syntax.

See the PHP manual, "complex (curly) syntax."

紫南 2024-08-03 13:21:30

我知道使用变量变量时的语法:

$userProp = 'id';
$this->_session->{'user_'.$userProp};

I know the syntax just when using variable variables:

$userProp = 'id';
$this->_session->{'user_'.$userProp};
缱绻入梦 2024-08-03 13:21:30

然而,这种语法可能有一个很大的优点,它通常是在多毛的东西领域,以及你可能想要避免的东西。

它允许您在变量名称中使用原本不允许的字符。

IE:

$this->object->{"hello world\0\n"} 
$this->object->{"function(){   this is a truely awful  name for a variable }"} 

Theres probably one big advantage of that syntax, however, its generally in the domain of hairy stuff, and things you probably want to avoid.

It permits you to use characters in variable names that are otherwise unpermitted.

ie:

$this->object->{"hello world\0\n"} 
$this->object->{"function(){   this is a truely awful  name for a variable }"} 
属性 2024-08-03 13:21:30

在您给出的示例中,没有真正的区别,并且 IMO $this->_session->user_id 应该使用,因为它更清晰。

大括号语法实际上的好处是通过构造成员变量名称的表达式来访问成员变量,例如 $this->_session->{'user_id' 。 $索引}

In the example you give, there's no real difference, and IMO $this->_session->user_id should be used because it's clearer.

What the curly brace syntax is actually good for is accessing a member variable by constructing an expression for its name, like $this->_session->{'user_id' . $index}.

残月升风 2024-08-03 13:21:30

你问题中的两个例子做了同样的事情。 PHP 允许您以多种方式访问​​成员数据/方法...

object->{'name_of_member'};

object->name_of_member;

$member = 'name_of_member';
object->$member;

The two examples in your question do the same thing. PHP allows you to access member data/methods in several ways...

object->{'name_of_member'};

object->name_of_member;

$member = 'name_of_member';
object->$member;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文