0

所以这是我的mixin类:

class AisisCore_Loader_Mixins {

    private $_classes;

    private $_class_objects = array();

    private $_methods = array();

    public function __construct(){
        $this->init();
    }

    public function init(){}

    public function setup($class){
        if(!is_array($class)){
            throw new AisisCore_Loader_LoaderException('Object passed in must be of type $class_name=>$params.');
        }

        $this->_classes = $class;
        $this->get_class_objects();
        $this->get_methods();    
    }

    public function get_class_objects(){
        foreach($this->_classes as $class_name=>$params){
            $object = new ReflectionClass($class_name);
            $object_name = get_class($object);
            $this->_class_objects[$object->name] = $object->newInstanceArgs($params);
        }
    }

    public function get_methods(){ 
        foreach($this->_class_objects as $class_object_name => $class_object){
            $this->_methods[$class_object_name] = get_class_methods($class_object);
        }

        return $this->_methods;
    }

    public function __call($name, $param = null){
        foreach($this->_methods as $class_name=>$methods){
            foreach($methods as $method){
                if($name === $method){
                    return $this->isParam($class_name, $method, $param);
                }
            }
        }

        throw new AisisCore_Loader_LoaderException("Method: " .$name. 
                            " does not exist or it's access is not public");
    }

    private function isParam($class_name, $method, $param){
        if($param != null){
            call_user_func(array($class_name, $method), $param);
        }else{
            call_user_func(array($class_name, $method));
        }        
    }
}

很简单的东西,加载一组类,允许你调用它们的函数等等,但是我们有一个新问题。似乎传递给 this 的类被实例化为静态的,因此它们的方法不能使用$this->它们使用self::它们是错误的。

让我们看一个例子来说明这一切是如何工作的:

class BaseBridge extends AisisCore_Loader_Mixins{

    public function __construct(){ 
        parent::construct();

        $this->setup(array('ClassB' => array()));
    }
}

让我们定义ClassB

class ClassB{

    public function __construct(){}

    public function some_method(){
        $this->_some_private_method();
    }

    private function _some_private_method(){}
}

非常基本的东西,所以让我们把它全部连接起来ClassA

class ClassA extends BaseBridge{

    public function __construct(){ 
        parent::__construct(); 
        $this->some_method();
    }
} 

快速回顾:我们有一个核心类,ClassA它扩展BaseBridge它是我们在一个或多个(意味着与更多)类之间扩展的桥梁类ClassA。在这种情况下,只是ClassB为了简单起见。

什么问题?看看,在ClassB做什么:$this->_some_private_method();是的,这将是巨大的和灾难性的失败。为什么?因为我得到了错误:Using $this when not in object context这让我很困惑,所以我把它改成:self::$_some_private_method();它就像一个魅力。

为什么?以及我必须更改或修复什么才能使其$this可以在通过 mixin 类实例化的类中使用?

4

1 回答 1

0

因此,通过一些细微的修改,我设法完成了这项工作。我怎么不相信具有多个参数的函数会起作用 - 感谢反馈。

class AisisCore_Loader_Mixins {

    private $_classes;

    private $_class_objects = array();

    private $_methods = array();

    public function __construct(){
        $this->init();
    }

    public function init(){}

    public function setup($class){
        if(!is_array($class)){
            throw new AisisCore_Loader_LoaderException('Object passed in must be of type $class_name=>$params.');
        }

        $this->_classes = $class;
        $this->get_class_objects();
        $this->get_methods();    
    }

    public function get_class_objects(){
        foreach($this->_classes as $class_name=>$params){
            $object = new ReflectionClass($class_name);
            $this->_class_objects[$object->name] = $object->newInstanceArgs($params);
        }
    }

    public function get_methods(){ 
        foreach($this->_class_objects as $class_object_name => $class_object){
            $this->_methods[$class_object_name] = get_class_methods($class_object);
        }

        return $this->_methods;
    }

    public function __call($name, $param = null){
        foreach($this->_methods as $class_name=>$methods){
            foreach($methods as $method){
                if($name === $method){
                    return $this->_is_param($class_name, $method, $param);
                }
            }
        }

        throw new AisisCore_Loader_LoaderException("Method: " .$name. 
                            " does not exist or it's access is not public");
    }

    private function _is_param($class_name, $method, $param){
        if($param != null){
            $this->_param_is_array($class_name, $method, $param);
        }else{
            call_user_func(array($this->_class_objects[$class_name], $method));
        }        
    }

    private function _param_is_array($class_name, $method, $param){
        if(is_array($param)){
            call_user_func_array(array($this->_class_objects[$class_name], $method), $param);
        }else{
          call_user_func(array($this->_class_objects[$class_name], $method, $param));  
        }        
    }
}

现在由此类注册的类中的函数可以使用$this->.

问题是我不确定多个基于参数的函数是否真的有效。

于 2013-10-09T17:06:01.790 回答