$this in HelperMethods

I use this HelperMethod (defined in my ApplicationController which extends PicoraController:


public function listItemsHelper() {
    $lists = $this->db->query("SELECT id, title FROM lists ORDER BY title DESC");
    foreach ($lists as $item) { /* bla bla */ }
    return "";

The ApplicationController has an attribute "db" which is an handle to an instance to my database-class. The Helper-Method was added with the addHelperMethod from the view:


$view = new PicoraView($this, $file, $params);
$view->addHelperMethod('listItems', array('ApplicationController', 'listItemsHelper'));

In the Template, I call the helper-method like this:


echo $this->listItems();

Now, PHP gives me the folowing error:

Fatal error: Using $this when not in object context in
/[...]/ApplicationController.php on line 32

Why can't I use $this in the helper-methods? Where the helperMethods called the static way? Or have i made a mistake?

Thanks for answers, Aaron

Posted June 1st, 2007 at 1:33pm by Aaron

I think it because only vars are passed to the view file.

extract(get_object_vars($this),EXTR_REFS);

and secondly this method can work fin because the method "addMethod" doen't exist:

 /**
 * @param mixed String method name, or array of method name => callback pairs
 * @param callback Callback function
 */
final public function addHelperMethod($method,$callback = false){
    if(is_array($method))
        foreach($method as $_method => $_callback)
            $this->addMethod($_method,$_callback);
    else
        $this->__methods__[$method] = $callback;
}

Posted June 3rd, 2007 at 11:31am by philippe

Login or Register to Post