PHP 中 $this->$variable 的语法不明确

发布于 2024-10-16 00:12:53 字数 893 浏览 4 评论 0原文

如果以前有人问过这个问题,请原谅,但我尝试搜索它但没有令人满意的结果。

我正在学习 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 技术交流群。

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

发布评论

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

评论(1

时光匆匆的小流年 2024-10-23 00:12:53

$this->$name$this->name 确实表示同一件事。第一个是使用本地作用域变量 $name 访问 $this 的字段,其名称是 $name 包含的任何内容,而第二个访问直接输入 name 字段。

例如,以下内容将输出 something

$foo = new stdClass;
$foo->bar = 'something';

$baz = 'bar';
echo $foo->$baz;

__get__set 的情况下,$name 包含名称在调用站点访问的属性;在您的情况下,myInteger

在您的示例中, __get__set 方法实际上是多余的,因为 $myA->myInteger 是公共的,可以直接访问。仅需要 __get__set 来捕获对类中未显式声明的属性的访问尝试。

例如,您可能有一个允许动态设置任意“属性”的支持数组:

class Foo
{
    private $_values = array();

    public function __get($key)
    {
        if (isset($this->_values[$key]))
        {
            return $this->_values[$key]
        }
    }

    public function __set($key, $value)
    {
        $this->_values[$key] = $value;
    }
}

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 the name field directly.

For example, the following will output something:

$foo = new stdClass;
$foo->bar = 'something';

$baz = 'bar';
echo $foo->$baz;

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:

class Foo
{
    private $_values = array();

    public function __get($key)
    {
        if (isset($this->_values[$key]))
        {
            return $this->_values[$key]
        }
    }

    public function __set($key, $value)
    {
        $this->_values[$key] = $value;
    }
}

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 $!

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