Returning a reference from a Class member?

Posted by nebukadnezzar on Stack Overflow See other posts from Stack Overflow or by nebukadnezzar
Published on 2010-06-10T10:51:32Z Indexed on 2010/06/10 11:12 UTC
Read the original article Hit count: 218

Filed under:
|

Hi, I've a class that basically looks like this:

class App {
    public function newTemplate($filename, $vars=array())
    {
        $smarty = new Smarty;
        $smarty->compile_dir = $this->template_compile_path;
        if(count($vars) > 0)
        {
            foreach($vars as $v_key => $v_name)
            {
                $smarty->assign($v_key, $v_name);
            }
        }
        return $smarty;
    }
}

However, when I create a Instance of 'App', the reference to $smarty seems broken, as every call to the membermethods don't seem to do anything:

$app = new App;
$tpl = $app->newTemplate("index.tmpl");
$tpl->assign("foo", "bar"); // {$foo} does not appear with "bar" in the template

Now I wonder why? Of course I tried to use references:

...
public function &newTemplate()
...

... But that doesn't work. Variable references don't seem to work either:

...
$tpl = &$app->newTemplate("index.tmpl");
...

What is causing PHP here not to return a proper reference? Help is very appreciated!

© Stack Overflow or respective owner

Related posts about php

Related posts about smarty