Page 1 of 1

[Solved] Beginner --> Returning a class call

Posted: Sun Sep 07, 2008 6:02 pm
by TheGoo
Hi Gang,

I'm new(ish) at PHP, and having a little trouble figuring something. Perhaps best explained by example.

Code: Select all

 
class queries {
  ...
  ...
  function assign_smarty($data) {
    foreach ($data as $key=>$value) {
      $smarty->assign($key, $value);
    }
  }
}
 
From within the class, $smarty->assign returns a "Call to a member function assign() on a non-object" error. I've tried "return $smarty->assign($key, $value)", but it doesn't seem to have any effect.

The call I'm using in the related page is just

Code: Select all

$queries->assign($data)
The code works fine when used directly on the page from which I'm (now) trying to call the class. Any suggestions most appreciated.

Lotsa thanks,
- The Goo

Re: Beginner --> Returning a class call

Posted: Sun Sep 07, 2008 6:09 pm
by ahowell
You need to make the $smarty object accessible to your class method:

Code: Select all

class queries {
  ...
  ...
    function assign_smarty($data) 
    {
        [color=#FF0000]global $smarty;
 
[/color]        foreach ($data as $key=>$value) {
            $smarty->assign($key, $value);
        }
    }
}

Re: Beginner --> Returning a class call

Posted: Sun Sep 07, 2008 7:06 pm
by TheGoo
*grin* Many thanks ahowell - much appreciated. Worked a treat. :-)