Here is my class that gets called on each page:
class ActionHandler {
var $smarty = NULL;
public function __construct() {
if($this->smarty == NULL){
$this->smarty = new Smarty();
$this->smarty->template_dir = TEMPLATE_DIR;
$this->smarty->compile_dir = COMPILE_DIR;
}
}
public function do_something($page_id) {
return $page_id + 1;
}
}
Now I have a custom plugin for smarty that I want to use in my template:
function smarty_function_something($params, &$smarty) {
return ActionHandler::do_something($params['page_id']);
}
However I get Fatal error: Using $this when not in object context.
I see why but don’t know how to get around this. Any ideas?
Try making the do_something a static member of
ActionHandlerAs your trying to access a non static method i *think that the __construct gets executed before the method is available, but as you have not created an instance of the object, the keyword
$thisdoes not exists.you have to create specific static methods. if your going
MyObject::SomeMethod($param)you should also take a look at Object Auto Loading and Auto Initializing objects via static methods.
also you don’t need to specifically define the value to
public static $smarty = NULL;as Null is a default value of any new variable, just dogoing a little more indepth with your problem you should add a singleton method like so..