成员变量的 PHP 花括号语法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1147937/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP curly brace syntax for member variable
提问by David Weinraub
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.
关于 SO 的第一个问题,它是一个真正的 RTM 候选者。但我向你保证,我已经看过了,但似乎找不到。当我错过了一件简单的事情时,我会很高兴地做一个#headpalm。
Trying to figure out Zend Framework and came across the following syntax:
试图找出 Zend Framework 并遇到以下语法:
$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.
我假设 _session 无关紧要,但将其包含在问题中,因为它可能不是。
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?
花括号只是试图包装复合变量名称 user_id 的清洁约定吗?或者它是某种特殊的存取器?
Any pointers into TFM so I can R up would be humbly appreciated.
任何指向 TFM 的指针,以便我可以 R up 将不胜感激。
Many thanks. Please be gentle.
非常感谢。请温柔点。
采纳答案by James Skidmore
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."
请参阅PHP 手册,“复杂(卷曲)语法”。
回答by Gumbo
I know the syntax just when using variable variables:
我知道使用变量变量时的语法:
$userProp = 'id';
$this->_session->{'user_'.$userProp};
回答by Kent Fredric
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:
IE:
$this->object->{"hello worldobject->{'name_of_member'};
object->name_of_member;
$member = 'name_of_member';
object->$member;
\n"}
$this->object->{"function(){ this is a truely awful name for a variable }"}
回答by chaos
In the example you give, there's no real difference, and IMO $this->_session->user_idshould be used because it's clearer.
在您给出的示例中,没有真正的区别,$this->_session->user_id应该使用IMO,因为它更清晰。
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}.
花括号语法实际上的好处是通过构造名称的表达式来访问成员变量,例如$this->_session->{'user_id' . $index}.
回答by Donnie DeBoer
The two examples in your question do the same thing. PHP allows you to access member data/methods in several ways...
你问题中的两个例子做同样的事情。PHP 允许您以多种方式访问成员数据/方法...
##代码##
