0

I am building a wordpress plugin in some MVC style and i have setup almost every thing but when using call_user_func to call the request action class, I am not able to use the $this with the requested class.

Here is my code so far...

    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);
        }
    }
}

The requested class goes here

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();


}

}

suppose i requested

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

please tell me what i am doing wrong..

Gaurav Mehra
  • 471
  • 7
  • 20
  • 1
    You are calling the method in a static context, where no `$this` exists. See [PHP Fatal error: Using $this when not in object context](http://stackoverflow.com/q/2350937) – Pekka Jun 17 '13 at 09:05
  • then what could be the possible solution in this case. i am using __autoload() function to include the classes. – Gaurav Mehra Jun 17 '13 at 09:18
  • I got it right thanks. I forgot to initialize the class object – Gaurav Mehra Jun 17 '13 at 09:26

1 Answers1

0

use $this->render() instead of $this->class->render();

another is: add ucfirst because your class name is in first letter capital and you are passing small case in arguments.

$this->controller = call_user_func_array(array(ucfirst($this->class), $this->method), $this->args)
skparwal
  • 1,086
  • 6
  • 15