[Solved] Beginner --> Returning a class call

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
TheGoo
Forum Newbie
Posts: 2
Joined: Sun Sep 07, 2008 5:33 pm

[Solved] Beginner --> Returning a class call

Post 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
Last edited by TheGoo on Sun Sep 07, 2008 7:06 pm, edited 1 time in total.
ahowell
Forum Newbie
Posts: 17
Joined: Mon Sep 01, 2008 9:18 pm

Re: Beginner --> Returning a class call

Post 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);
        }
    }
}
TheGoo
Forum Newbie
Posts: 2
Joined: Sun Sep 07, 2008 5:33 pm

Re: Beginner --> Returning a class call

Post by TheGoo »

*grin* Many thanks ahowell - much appreciated. Worked a treat. :-)
Post Reply