Tag Archives: PHP $this Error

Using $this when not in object context in (How to Fix)

Error message: The $this reference has no context
Reason: In PHP5, $this cannot be used in static methods of static declarations; you need to use self to refer to methods or variables in the current class.Example code is as follows:

<?php
namespace syhl\admin\page\record;  // namespace

require_once  dirname(__FILE__).'/../../../../common/smarty_loader.php';  

class record {
  
    
     public static function exec($smarty) {    
        
        $ttr=self::getres();
        $smarty->assign("arr",$ttr);      
        $smarty->display ( 'rec_mgr.html' );
    }
   function getres(){
      $arr = array (  
       "1" => 'test',  
       '2' => 'me',  
       array (  
        "3" => "beij",  
        "4" => "zz"  
       ),  
       array (  
        "5",  
        "6" => "ewrwer",  
        "7" => "ssss"  
       )  
      );  
      return $arr;
    }
}
record::exec($smarty);

?>

 

$this(getRes () method in the sample code) is not allowed in the referenced method.