不在对象上下文中时使用$ this。 wordpress插件

时间:2013-06-17 09:03:36

标签: php wordpress

我正在构建一个MVC风格的wordpress插件,我已经设置了几乎所有的东西,但是当使用call_user_func来调用请求动作类时,我无法将$ this与请求的类一起使用。

到目前为止,这是我的代码......

    class Action{

    protected $class;
    protected $method;
    protected $args = array();
    protected $template='settings/index';

    public function __construct() {
       $this->getRequest();

    }


    public function getRequest(){
        if(isset($_GET['c'])){
            $route = $_GET['c'];
            $parts = explode('/',$route);
            if(isset($parts[0]))
            $this->class = $parts[0];

            if(isset($parts[1]))
            $this->method = $parts[1];

            if(isset($parts[2]))
            $this->args[] = $parts[2];

            if(isset($parts[3]))
            $this->args[] = $parts[3];
        }
    }

    public function render(){

        extract($this->data);

        ob_start();

        require(LINKWAG_VIEW .DS. $this->template . P);

        $this->output = ob_get_contents();

        ob_end_clean();

        return $this;
    }

    public function output(){
        echo $this->output;
    }
}



class Grv extends Action{
    public function __construct() {
         parent::__construct();
        add_action('admin_menu', array($this,'setup'));
    }

    public function setup(){
         add_menu_page( 'LinkWag', 'LinkWag', 'manage_options', 'linkwag',array($this,'execute'), plugins_url( 'myplugin/images/icon.png' ), 6 ); 



    }

    public function  execute(){
        if($this->class){ 
           $this->controller = call_user_func_array(array($this->class,$this->method), $this->args);
        }
    }
}

请求的课程在这里

class Settings extends Grv{
public function __construct() {
    //parent::__construct();
}

public function view($id=false){
    $this->template = 'settings/view'; // using $this here craetes a fatal error

   $this->render();


}

}

假设我要求

admin.php?page=grv&c=settings/view/2

请告诉我我做错了什么..

1 个答案:

答案 0 :(得分:0)

使用 $this->render() 代替$this->class->render();

另一个是:添加ucfirst,因为你的类名是首字母大写,而你在参数中传递小例子。

$this->controller = call_user_func_array(array(ucfirst($this->class), $this->method), $this->args)