-1

I am seeing some weird error on my client website. It's code someone else made for him for his application. The error message is saying that $this when not in object context but the Class has been extended where from App.

Please help out.

Error

Fatal error: Using $this when not in object context in contactController.php on line 6

contactController.php

class contactController extends App{    
    public function index(){
        $this->view('content'); //error mmessage is pointing here
    }
}

app.php

class App{

    public $controller;
    public $method;
    public $params = [];

    public function view( $file ){
        include( site_path() . '/views/' . $file . '.php' );
    }

    public function model( $file ){
        require_once( site_path() . '/models/' . $file . '.php' );
    }

    public function engine(){
        global $core_current_ControllerMethod, $core_current_controller, $core_current_method;
        //Get the current controller and method from a helper function
        $get_wc_cm = get_wc_cm();
        //Assign it to the global variable
        $core_current_ControllerView = $get_wc_cm;
        //Seperate the controller and method
        $cm = explode('@', $get_wc_cm);
        $controller = !empty($cm[0])? $cm[0]: null; // This is the controller
        $method = !empty($cm[1])? $cm[1]: null; // This is the method
        //Assign it to the global varaible
        $core_current_controller = $controller;
        $core_current_method = $method;
        //Assign it to the class variable
        $this->controller = $controller;
        $this->method = $method;

        $ControllerFile = site_path(). '/controllers/' . $this->controller . '.php';
        if( file_exists($ControllerFile) ){
            require_once($ControllerFile);
            new $this->controller;
            $callback = is_callable(array($this->controller, $this->method), false);
            if( $callback ){
                call_user_func_array([$this->controller, $this->method], [$this->params]);
            }
        }
    }

}

$app = (new App)->engine();
tereško
  • 58,060
  • 25
  • 98
  • 150
Red Virus
  • 1,633
  • 3
  • 25
  • 34

1 Answers1

1

Try to change :

class contactController extends App{    
    public function index(){
        $this->view('content'); //error mmessage is pointing here
    }
}

To :

class contactController extends App{    
    public function index(){
        parent::view('content'); //error mmessage is pointing here
    }
}
DevLoots
  • 747
  • 1
  • 6
  • 21
  • Wouldn't it make sense to first understand _why_ the error is thrown, before _trying_ something else? Programming is not a guessing game... – arkascha Mar 25 '16 at 07:30
  • @Pierre your solution works. It would nice if you can explain why was it happening. if you can. – Red Virus Mar 25 '16 at 07:33
  • I think that the difference is that parent:: calls the parent's implementation while $this-> calls the child's implementation if the child has its own implementation instead of inheriting it from the parent. The idea is to avoid the child class to seek re-implemented method in the parent class that does not exist . You can use $this if you have a view method in the child class. – DevLoots Mar 25 '16 at 07:39
  • You can use $this, but you should create a constructor for contactController before. – DevLoots Mar 25 '16 at 07:50