从包含文件调用私有或受保护的方法

| myclass.php
class myclass {

private $name;

public function showData(){
    include_once \"extension.php\";

    otherFunction($this);

}

private function display(){
    echo \"hello world!\";
}

}
extension.php
function otherFunction($obj){

    if(isset($obj){
   $obj->display();
    }

}
好的,这就是问题所在,对于某些人来说,很明显,我正在从包含文件中调用私有方法,这显然会引发错误。我的问题是: 1.有没有一种包含文件的方法     可以使用外部函数来调用     私人方法? 2.如何使用包含的文件来     访问私有方法并通过执行     所以将我的功能扩展到另一个     文件而不制作我的班级文件     功能many肿? 3.那有可能吗? 谢谢     
已邀请:
        如果您使用的是PHP 5.3,则可以。 这就是反射。根据您的需要,您需要ReflectionMethod http://us3.php.net/manual/zh/class.reflectionmethod.php 这是一个例子
<?php

//  example.php
include \'myclass.php\';

$MyClass = new MyClass();

//  throws SPL exception if display doesn\'t exist
$display = new ReflectionMethod($MyClass, \'display\');

//  lets us invoke private and protected methods
$display->setAccesible(true);

//  calls the method
$display->invoke();

}
显然,您希望将其包装在try / catch块中,以确保处理异常。     

要回复问题请先登录注册