tags:

views:

22

answers:

2

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?

+1  A: 

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;
    }
}
RobertPitt
No, it would just generate a warning saying that you're not supposed to call a non-static method statically. It won't instantiate an object.
Adam Backstrom
yea, was not 100% sure so I placed *think in there.
RobertPitt
thanks, is there anyway to make this work without having to make everything static?
fire
you can just declare a static function
RobertPitt
yes but the example I gave was very simple, when do_something starts referencing other functions within the class those other functions need to become static or I get the same error
fire
then you should incorporate a singleton, ive update with an example, then within do_something you can use $this to access methods without having to make static members.
RobertPitt
A: 

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

Adam Backstrom