在对象外部修改对象的私有属性和私有方法

in 前端 with 0 comment

使用bind方法

class A
{
    private $c = 1;


    public function c1()
    {
        echo $this->c;
    }
}

$out = new A();

$func = Closure::bind(function () {
    $this->c = 5;
}, $out, 'A');

$func();

$out->c1(); // 5

composer 的autoload_static.php 里面就用到了这种方式。

参考:js bind

Comments are closed.