成员变量的PHP大括号语法 [英] PHP curly brace syntax for member variable
问题描述
关于SO的第一个问题,它是真正的RTM候选人.但是我保证你我已经看过了而且似乎找不到.当我发现我错过了一件简单的事情时,我会很高兴地做一个#headpalm.
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.
试图弄清楚Zend Framework并遇到以下语法:
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
我假设_session无关紧要,但由于可能与之无关,因此将其包含在问题中.
I'm assuming that the _session is irrelevant, but including it in the question since it may not be.
花括号是否只是用来包装复合变量名称user_id的清洁约定?还是某种特殊的访问器?
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?
任何指向TFM以便我可以R向上运行的指针都将由衷地感谢.
Any pointers into TFM so I can R up would be humbly appreciated.
非常感谢.请保持温柔.
Many thanks. Please be gentle.
推荐答案
大括号用于显式指定变量名的结尾.例如:
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.
请参见 PHP手册,复杂(卷曲)语法".
See the PHP manual, "complex (curly) syntax."
这篇关于成员变量的PHP大括号语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!