繁体   English   中英

PHP:如何通过反射或其他方式从子 class 获取父抽象 class 私有属性

[英]PHP: How to get parent abstract class private property from child class with reflections or with another way

我有这样的情况。

所以我们有具有私有$secret属性的抽象father

abstract class father{
    private $secret = 'my_secret';
}

并且还有一个孩子class

class child extends father{
    public function getFatherSecret(){
        // some code to get a private prop
    }
}

我不知道也许这是不可能的。

因此,我需要获得一个父私有属性,如Closure 绑定方法或反序列化方法,这些方法已经在一些 PHP 文档中得到充分解释。

虽然我对这种方法的使用有各种保留意见,但总是有回答问题的原则(即使是纯学术意义上的)。

所以...

abstract class father{
    private $secret = 'my_secret';
}

class child extends father{
    public function getFatherSecret(){
        $closure = Closure::bind(function (father $f) { return $f->secret; }, 
            null, "father");

        print_r($closure($this));
    }
}

$c = new child();
$c->getFatherSecret();

给...

my_secret

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM