从闭包内访问私有变量

发布于 2024-09-19 18:21:20 字数 806 浏览 4 评论 0原文

我试图从闭包中引用对象的私有变量。下面的代码似乎可以工作,但它抱怨 Fatal error: Cannot access self:: when no class scope is active in test.php on line 12Fatal error: Using $this when不在 test.php 第 20 行的对象上下文中。

任何想法如何使用闭包实现相同的结果,同时保持变量私有并且不创建辅助函数(击败私有变量的整个想法)。

class MyClass
{

    static private $_var1;
    private $_var2;

    static function setVar1( $value )
    {
        $closure = function () use ( $value ) {
            self::$_var1 = $value;
        };
        $closure();
    }

    function setVar2( $value )
    {
        $closure = function () use ( $value ) {
            $this->_var2 = $value;
        };
        $closure();
    }

}

MyClass::setVar1( "hello" ); //doesn't work

$myclass = new MyClass;
$myclass->setVar2( "hello" ); //doesn't work

I'm trying to reference a private variable of an object from within a closure. The code below would seem to work, but it complains Fatal error: Cannot access self:: when no class scope is active in test.php on line 12 and Fatal error: Using $this when not in object context in test.php on line 20.

Any ideas how to accomplish the same results using a closure while keeping the variables private and without making helper functions (defeating the whole idea of a private variable).

class MyClass
{

    static private $_var1;
    private $_var2;

    static function setVar1( $value )
    {
        $closure = function () use ( $value ) {
            self::$_var1 = $value;
        };
        $closure();
    }

    function setVar2( $value )
    {
        $closure = function () use ( $value ) {
            $this->_var2 = $value;
        };
        $closure();
    }

}

MyClass::setVar1( "hello" ); //doesn't work

$myclass = new MyClass;
$myclass->setVar2( "hello" ); //doesn't work

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

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

发布评论

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

评论(3

↘人皮目录ツ 2024-09-26 18:21:20

编辑注意,这个答案最初是针对PHP5.3及更早版本的,现在是可能的。有关最新信息,请参阅此答案


这不是直接可能的。特别是,闭包没有关联的作用域,因此它们无法访问私有和受保护的成员。

但是,您可以使用参考:

<?php
class MyClass
{

    static private $_var1;
    private $_var2;

    static function setVar1( $value )
    {
        $field =& self::$_var1;
        $closure = function () use ( $value,  &$field ) {
            $field = $value;
        };
        $closure();
    }

    function setVar2( $value )
    {
        $field =& $this->_var2;
        $closure = function () use ( $value, &$field ) {
            $field = $value;
        };
        $closure();
    }

}

MyClass::setVar1( "hello" );

$myclass = new MyClass;
$myclass->setVar2( "hello" );

Edit to note, this answer was originally meant for PHP5.3 and earlier, it's possible now. For current information, see this answer.


This is not directly possible. In particularly, closures have no associated scope, so they cannot access private and protected members.

You can, however, use references:

<?php
class MyClass
{

    static private $_var1;
    private $_var2;

    static function setVar1( $value )
    {
        $field =& self::$_var1;
        $closure = function () use ( $value,  &$field ) {
            $field = $value;
        };
        $closure();
    }

    function setVar2( $value )
    {
        $field =& $this->_var2;
        $closure = function () use ( $value, &$field ) {
            $field = $value;
        };
        $closure();
    }

}

MyClass::setVar1( "hello" );

$myclass = new MyClass;
$myclass->setVar2( "hello" );
溇涏 2024-09-26 18:21:20

从 PHP 5.4.0 开始可以实现这一点

class test {
    function testMe() {
        $test = new test;
        $func = function() use ($test) {
            $test->findMe();        // Can see protected method
            $test::findMeStatically();  // Can see static protected method
        };
        $func();
        return $func;
    }

    protected function findMe() {
        echo " [find Me] \n";
    }

    protected static function findMeStatically() {
        echo " [find Me Statically] \n";
    }
}

$test = new test;
$func = $test->testMe();
$func();        // Can call from another context as long as 
            // the closure was created in the proper context.

This is possible starting in PHP 5.4.0

class test {
    function testMe() {
        $test = new test;
        $func = function() use ($test) {
            $test->findMe();        // Can see protected method
            $test::findMeStatically();  // Can see static protected method
        };
        $func();
        return $func;
    }

    protected function findMe() {
        echo " [find Me] \n";
    }

    protected static function findMeStatically() {
        echo " [find Me Statically] \n";
    }
}

$test = new test;
$func = $test->testMe();
$func();        // Can call from another context as long as 
            // the closure was created in the proper context.
少跟Wǒ拽 2024-09-26 18:21:20

闭包没有 $thisself 的概念——它们不以这种方式绑定到对象。这意味着您必须通过 use 子句传递变量...类似于:

$_var1 =& self::$_var1;
$closure = function() use ($value, &$_var1) {
  $_var1 = $value;
};

$_var2 =& $this->_var2;
$closure = function() use ($value, &$_var2) {
  $_var2 = $value;
};

我还没有测试上面的代码,但我相信它是正确的。

Closures have no concept of $this or self -- they are not tied to objects in that way. This means that you would have to pass the variables through the use clause... something like:

$_var1 =& self::$_var1;
$closure = function() use ($value, &$_var1) {
  $_var1 = $value;
};

$_var2 =& $this->_var2;
$closure = function() use ($value, &$_var2) {
  $_var2 = $value;
};

I haven't tested the above code, but I believe it to be correct.

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