Using $this when not in object context in Yii

anu picture anu · May 2, 2013 · Viewed 11k times · Source

Iam getting an error like Fatal error: Using $this when not in object context in ..\controllers\ServiceRequestController.php on line 661 when calling a view file using controller action from EasyTabs extension.
Iam calling the controller action like this in the view file
ServiceRequestController::actionTest();
and in controller

     public static function actionTest()  
   {
        $this->redirect('test');
    }

How can I get rid of this error?? When I googled, I found that $this cannot be used in a static method. . So I tried out using
$model = new ServiceRequest(); $model->Test(); in my view file.But it shows the error as ServiceRequest and its behaviors do not have a method or closure named "actionTest". Can anyone help me fixing the error? Thanks in advance I tried to fix using this link . But I think Iam mistaken. PHP Fatal error: Using $this when not in object context

Answer

Michael Härtl picture Michael Härtl · May 2, 2013

Do not use the keyword static when you define the action.

You can read more about static methods and properties here:

http://de1.php.net/manual/en/language.oop5.static.php