2013-05-02 121 views
1

荫对象方面得到了如下的错误Fatal error: Using $this when not in object context in ..\controllers\ServiceRequestController.php on line 661调用使用从EasyTabs扩展控制器动作视图文件时。
荫调用视图文件
ServiceRequestController::actionTest();
和控制器

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

我怎样才能摆脱这种错误的这样的控制器操作?当我Google搜索时,我发现$this cannot be used in a static method.。所以我尝试了用在我看来
$model = new ServiceRequest(); $model->Test(); file.But它显示的错误为ServiceRequest and its behaviors do not have a method or closure named "actionTest". 谁能帮我修复错误?在此先感谢 我试图修复使用此链接。但我认为Iam错了。 PHP Fatal error: Using $this when not in object context

回答

0

当使用$ this关键字需要在某些时候已经被实例化对象。如果你有一个类:

class Example { 

    private $word = 'hello'; 

    public static function hello() { 
     echo 'I can be called by writing:'; 
     echo 'Example::yo()'; 
    } 

    public function world() { 
     echo 'If this class has been instantiated, (new Example;)'; 
     echo '$this->word will be hello'; 
     echo 'If this class hasn't been instantiated, you will get your error'; 
    } 
} 

一个类的特定实例可以从它内部的方法引用为$ this。

你可以写:

$example1 = new Example; 
$example2 = new Example; 
$example3 = new Example; 

里面的$例1,$这将指向同一个东西,$例1是指从它的外面。所以,你可以改变$例1的$字,像这样:

$example1->word = 'yo'; // from outside of the class 
$this->word = 'yo';  // from inside of the class 

之前你实例化的类使用new关键字虽然没有$这种存在呢。该班是一个蓝图,与您共创使用,然后可以从通过$ this内所指的新的关键字出来的对象。

+0

我怎么能实现它在这种情况下? – anu 2013-05-02 09:20:15

0

试试这个

self::actionTest(); 

代替

ServiceRequestController::actionTest(); 
0

我通过修正修正了这个错误yii2我URL例如

http://localhost/e-compare2/backend/web/index.php?r=crud/make 
相关问题