源代码:
$reflection_class = new ReflectionClass('Controllercommonfooter');
var_dump($reflection_class);                                              #output_1
$reflection_method = $reflection_class->getMethod('index');
var_dump($reflection_method);                                             #output_2
var_dump($reflection_method->isProtected());                              #output_3
输出结果是:
object(ReflectionClass)[36]
  public 'name' => string 'ControllerCommonFooter' (length=22)object(ReflectionMethod)[40]
  public 'name' => string 'index' (length=5)
  public 'class' => string 'ControllerCommonFooter' (length=22)boolean true
我通过var_dump($reflection_method->isProtected()); 确定类Controllercommonfooter的方法index的访问性是protected。


奇怪的是,下面的代码正常运行了,我搞不明白:
$file = '指向Controllercommonfooter类的文件路径';
$class = 'Controllercommonfooter';
$method = 'index';
$args = array();
//run
$controller = new $class();
$controller->$method($args);               #正常运行了,有返回结果。我想请大家解释一下设置为protected 的类方法在什么情况下可以这样直接调用?????
我想请大家解释一下设置为protected 的类方法在什么情况下可以这样直接调用?????
我想请大家解释一下设置为protected 的类方法在什么情况下可以这样直接调用?????
我想请大家解释一下设置为protected 的类方法在什么情况下可以这样直接调用?????
我想请大家解释一下设置为protected 的类方法在什么情况下可以这样直接调用?????
我想请大家解释一下设置为protected 的类方法在什么情况下可以这样直接调用?????
谢谢!
谢谢!
谢谢!
万分谢谢!!!

解决方案 »

  1.   

    class Controllercommonfooter {
      protected function index() {
        echo 'abcd';
      }
    }$reflection_class = new ReflectionClass('Controllercommonfooter');
    var_dump($reflection_class); #output_1
    $reflection_method = $reflection_class->getMethod('index');
    var_dump($reflection_method); #output_2
    var_dump($reflection_method->isProtected()); #output_3$class = 'Controllercommonfooter';
    $method = 'index';
    $args = array();$controller = new $class();
    $controller->$method($args); Fatal error: Call to protected method Controllercommonfooter::index() from context '' in ...
      

  2.   

    ReflectionMethod::isProtected ( void )
    TRUE if the method is protected, otherwise FALSE
    首先确定这个返回的是TRUE
    其次看清有无子类重载此方法
    protected只了类内或间调用,非外部调用
    想调用,一是子类重载此方法
    二是写一个方法取得内容,就像__GET与__SET差不多
    protected test(){echo "TEST";}
    public getTest(){$this->test();}
    可以外部直接调用的我还未见过,这不就违反了protected的初衷了嘛
      

  3.   

    PHP5.2.6下确认会报Fatal error
      

  4.   

    我写了个简化的版本,大家看一下这是什么原因:<?php
    class Action{
    private $class;
    private $method;
    public function __construct($route)
    {
    list($this->class, $this->method) = explode('/', $route);
    }

    public function getClass()
    {
    return $this->class;
    }

    public function getMethod()
    {
    return $this->method;
    }
    }class Controller
    {
    protected $children = array();
    protected $output;
    public function __construct() {}

    protected function getChild($child) {
    $action = new Action($child);
    $class = $action->getClass();
    $method = $action->getMethod();
    $controller = new $class(); /*********注意这里*********/
    $controller->$method(); /*********注意这里*********/
    return $controller->output;
    }

    public function render() {
    foreach ($this->children as $child) {
    $this->data[basename($child)] = $this->getChild($child);
    }
           $this->output .= implode('<br />------------------<br />', $this->data);
    //echo $this->data[basename($child)];
          
    return $this->output;
    }

    public function output()
    {
    return $this->output;
    }
    }class Kcontroller extends Controller
    {
    public function index()
    {
    $this->children = array('footer/footer', 'header/header');
    }
    }class Footer extends Controller
    {
    protected function footer()
    {
    $this->output = 'This is Page Footer!!!';

    }
    }class Header extends Controller
    {
    protected function header()
    {
    $this->output = 'This is Page Header!!!';
    }
    }
    $k = new Kcontroller();
    $k->index();
    $k->render();
    echo $k->output();
      

  5.   

    根本不是你描述的问题protected的方法getChild仅被类内部调用
      

  6.   

    echo $k->output();
    你把这个function output改为protected立马报错
    这就是protedted保护的意思,只能在类内调用不可外部调用
      

  7.   

    我做了下测试,故意在Footer类和Header类中添加了个私有方法:
    private function ppt()
    {
    echo 'blah blah';
    }
    然后在Controller类的getChild方法中添加:
    $controller->ppt();目的是为了显示当程序执行到哪里时的上下文环境,结果显示的是:
    Fatal error: Call to private method Footer::ppt() from context 'Controller'
    PHP手册里有这么一段:
    Members declared protected can be accessed only within the class itself and by inherited and parent classes.难道指的是这个。
    Members declared protected can be accessed within the parent classes。代码如下:
    <?php
    class Action{
    private $class;
    private $method;
    public function __construct($route)
    {
    list($this->class, $this->method) = explode('/', $route);
    }

    public function getClass()
    {
    return $this->class;
    }

    public function getMethod()
    {
    return $this->method;
    }
    }class Controller
    {
    protected $children = array();
    protected $output;
    public function __construct() {}

    protected function getChild($child) {
    $action = new Action($child);
    $class = $action->getClass();
    $method = $action->getMethod();
    $controller = new $class(); /*********注意这里*********/
    $controller->$method(); /*********注意这里*********/
    $controller->ppt();
    return $controller->output;
    }

    public function render() {
    foreach ($this->children as $child) {
    $this->data[basename($child)] = $this->getChild($child);
    }
           $this->output .= implode('<br />------------------<br />', $this->data);
    //echo $this->data[basename($child)];
          
    return $this->output;
    }

    public function output()
    {
    return $this->output;
    }
    }class Kcontroller extends Controller
    {
    public function index()
    {
    $this->children = array('footer/footer', 'header/header');
    }
    }class Footer extends Controller
    {
    protected function footer()
    {
    $this->output = 'This is Page Footer!!!';

    }

    private function ppt()
    {
    echo 'blah blah';
    }
    }class Header extends Controller
    {
    protected function header()
    {
    $this->output = 'This is Page Header!!!';
    }

    private function ppt()
    {
    echo 'blah blah';
    }
    }
    $k = new Kcontroller();
    $k->index();
    $k->render();
    echo $k->output();
      

  8.   

    Members declared protected can be accessed within the parent classes声明为受保护成员可以访问在父类
      

  9.   

    你举的这个例子,刚好是另一个权限private这个是完全私有的,级别最高无法被继承和protected有很大区别
    你了解一下php public protected private作用域就明白了