小编Pab*_*res的帖子

在PHP中从类外部调用私有方法和私有属性

我想在非常罕见的特定情况下从类外部访问私有方法和变量.

我已经看到尽管使用了内省,但这是不可能的.

具体案例是下一个案例:

我想要这样的东西:

class Console
{
    final public static function run() {

        while (TRUE != FALSE) {
            echo "\n> ";
            $command = trim(fgets(STDIN));

            switch ($command) {
                case 'exit':
                case 'q':
                case 'quit':
                    echo "OK+\n";
                    return;
                default:
                    ob_start();
                    eval($command);
                    $out = ob_get_contents();
                    ob_end_clean();

                    print("Command: $command");
                    print("Output:\n$out");         

                    break;
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这个方法应该能够像这样注入代码中:

Class Demo
{
    private $a;

    final public function myMethod()
    {
        // some code
        Console::run();
        // some other code
    }

    final public function myPublicMethod()
    {
        return "I can run …
Run Code Online (Sandbox Code Playgroud)

php visibility introspection

16
推荐指数
3
解决办法
3万
查看次数

标签 统计

introspection ×1

php ×1

visibility ×1