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 ActionHandler
class ActionHandler {
public static $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 static function do_something($page_id)
{
return $page_id + 1;
}
}
As 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 $this
does 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 do
public static $smarty;
going a little more indepth with your problem you should add a singleton method like so..
class ActionHandler
{
public static $smarty;
public static $singleton;
public function __construct()
{
if($this->smarty == NULL)
{
$this->smarty = new Smarty();
$this->smarty->template_dir = TEMPLATE_DIR;
$this->smarty->compile_dir = COMPILE_DIR;
}
}
public static GetSingleton()
{
if(self::$singleton == null)
{
self::$singleton = new ActionHandler();
}
return self::$singleton;
}
public static function do_something($page_id)
{
$_this = self::GetSingleton();
return $page_id + 1;
}
}
You omitted a few pieces of code: instantiation of either the Smarty or ActionHandler object, registration of the template function, template content, and Smarty::display()
call, but in my own testing your code works fine. In none of your code do you attempt to use $this
while not in an object context.
If you have additional code to post (preferably, the full reduction that still triggers the error) that may help with debugging.
smarty-test.php:
<?php
include 'Smarty.class.php';
class ActionHandler {
var $smarty = NULL;
public function __construct() {
if($this->smarty == NULL){
$this->smarty = new Smarty();
$this->smarty->template_dir = __DIR__ . '/t';
$this->smarty->compile_dir = __DIR__ . '/tc';
$this->smarty->plugins_dir = __DIR__ . '/plugins';
}
}
public function do_something($page_id) {
return $page_id + 1;
}
}
$ah = new ActionHandler;
$ah->smarty->display('index.tpl');
plugins/function.something.php:
<?php
function smarty_function_something($params, &$smarty) {
return ActionHandler::do_something($params['page_id']);
}
t/index.tpl:
Test: {something page_id=1}
Output:
Test: 2