PHP如何检测是否从公共或私有范围调用方法?

How can you detect if a method is being called from within a public, private or protected scope?

For example ...

class Foo {

    public function getPassword(){
        $scope = [??????];
        switch($scope){
            case 'public':
                return "*****";
                break;
            case 'protected': case 'private':
                return "IamPassword";
                break;
        }
    }
}

Inside the class I might need a property that might not be visible for the template engine but accessible by the class.

First of all I would strongly recommend you to redesign your code as soon as possible. But nevertheless your question seemed interesting to me that is why you may try this:

    $scopeIsInner = false;
    $exception = new Exception();
    $trace = $exception->getTrace();
    $class = $trace[1]['class'];
    if ($class == __CLASS__) {
        $method = $trace[1]['function'];
        $reflect = new ReflectionObject($this);
        $methodList = $reflect->getMethods(ReflectionMethod::IS_PROTECTED | ReflectionMethod::IS_PRIVATE);
        foreach ($methodList as $reflectionMethod) {
            if ($method == $reflectionMethod->name) {
                $scopeIsInner = true;
                break;
            }
        }
    }
    var_dump($scopeIsInner);

P.S. I would never use this code in my own application