PHP 中 $this->$variable 的语法不明确
如果以前有人问过这个问题,请原谅,但我尝试搜索它但没有令人满意的结果。
我正在学习 PHP(来自 C++ 背景)并且遇到了以下歧义。以下两段代码的工作原理完全相同:
class A
{
public $myInteger;
public function __get($name)
{
return $this->$name;
}
public function __set($name, $value)
{
$this->$name = $value;
}
}
也就是说
class A
{
public $myInteger;
public function __get($name)
{
return $this->name;
}
public function __set($name, $value)
{
$this->name = $value;
}
}
,在类方法中 $this->$name
和 $this->name
具有完全相同的功能相同的功能。我发现这有点令人困惑,特别是考虑到如果添加以下代码,
$myA = new A();
$myA->myInteger = 5;
$hereInt = $myA->myInteger;
echo "<p>" . $hereInt . "</p>";
则仅在 myInteger
$ 时才有效>。有人可以解释一下这背后的理由吗?
Please excuse me if this question has been asked before, but I tried searching for it with no satisfactory results.
I'm learning PHP (coming from a C++ background) and have come across the following ambiguity. The following two bits of code work exactly the same:
class A
{
public $myInteger;
public function __get($name)
{
return $this->$name;
}
public function __set($name, $value)
{
$this->$name = $value;
}
}
and
class A
{
public $myInteger;
public function __get($name)
{
return $this->name;
}
public function __set($name, $value)
{
$this->name = $value;
}
}
that is, in the class methods $this->$name
and $this->name
have the exact same function. I'm finding this a bit confusing, especially when considering that if you add the following code,
$myA = new A();
$myA->myInteger = 5;
$hereInt = $myA->myInteger;
echo "<p>" . $hereInt . "</p>";
it only works if there is no $
before myInteger
. Could someone please explain the rationale behind this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
$this->$name
和$this->name
确实不表示同一件事。第一个是使用本地作用域变量$name
访问$this
的字段,其名称是$name
包含的任何内容,而第二个访问直接输入name
字段。例如,以下内容将输出
something
:在
__get
和__set
的情况下,$name
包含名称在调用站点访问的属性;在您的情况下,myInteger
。在您的示例中,
__get
和__set
方法实际上是多余的,因为$myA->myInteger
是公共的,可以直接访问。仅需要__get
和__set
来捕获对类中未显式声明的属性的访问尝试。例如,您可能有一个允许动态设置任意“属性”的支持数组:
PHP 语法的这方面有些令人困惑的一件事是
$
在类中的字段声明之前,但访问该字段时没有任何内容。访问静态字段的语法使情况更加复杂,确实需要$
!$this->$name
and$this->name
do not mean the same thing. The first is using a locally scoped variable$name
to access the field of$this
whose name is whatever$name
contains, while the second accesses thename
field directly.For example, the following will output
something
:In the case of
__get
and__set
,$name
contains the name of the property that was accessed at the call site; in your case,myInteger
.In your example, the
__get
and__set
methods are actually superfluous, since$myA->myInteger
is public and can be accessed directly.__get
and__set
are only needed to catch access attempts to a property that is not declared explicitly in the class.For example, you might have a backing array that allows arbitrary "properties" to be set dynamically:
One thing that's somewhat confusing about this aspect of PHP's syntax is that a
$
precedes a field declaration in a class, but there is none when accessing that field. This is compounded by the syntax for accessing static fields, which does require a$
!