Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

可以访问Controller里面定义的私有方法? #76

Open
flashjay opened this issue Dec 7, 2013 · 3 comments
Open

可以访问Controller里面定义的私有方法? #76

flashjay opened this issue Dec 7, 2013 · 3 comments

Comments

@flashjay
Copy link

flashjay commented Dec 7, 2013

qq20131207-2

如图,正常情况是不应该这么写的,但是类里面的private方法可以被访问还是让人有点困惑。

@laruence
Copy link
Owner

laruence commented Dec 7, 2013

恩. 有道理...

@eicesoft
Copy link

eicesoft commented Dec 7, 2013

其实PHP可以在运行时更改对象的访问级别, 比如把private改为可以访问. 用反射完全可以做到.

参考一下之前我写的一个简单代码:

    $ref = new \ReflectionObject($action);
    $properties = $ref->getProperties();
    foreach ($properties as $propertie) {
        $pname = $propertie->getName();

        if (substr($pname, strlen($pname) - 7) == 'Service') {
            $propertie->setAccessible(true);
            $propertie->setValue($action, new ServiceProxy($pname));
        }
    }

目的是把Service的私有或者公有成员变量赋值一个ServiceProxy, 在业务开始调用的时候利用Proxy调用具体的Service.

@lagolas
Copy link

lagolas commented Nov 27, 2017

同问,为啥定义为private可以访问

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants