problems with Singleton and Visibilty?

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
joncampbell
Forum Newbie
Posts: 24
Joined: Fri Mar 11, 2005 12:57 pm
Location: Irvine, California, USA

problems with Singleton and Visibilty?

Post by joncampbell »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I have a singleton pattern I am using, and when I declare it in the construct function I get errors when I use it in other functions in the class... example:

Code: Select all

class Example {

        public          $example_error_message;
        private         $db;

        /* Constructor */
        public function __construct() {

                /* DB Support Object */
                $db = DB::Singleton();

                /* Custom Errors Array */
                $this->example_error_message = array();

        }

        public function ExampleView() {

                $db->ExampleDBfunction();

        }

}
produces this error: Fatal error: Call to a member function ExampleDBfunction() on a non-object in /folder/example_functions.php on line __LINE__

any suggestions as to why this is happening, I can fix the problem by re-assigning the $db object in each function, but I don't think I should have to do this.

Thank you in advance for any help you provide. :)


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

$db should be $this->db in the constructor.
joncampbell
Forum Newbie
Posts: 24
Joined: Fri Mar 11, 2005 12:57 pm
Location: Irvine, California, USA

Post by joncampbell »

That did the trick, thank you... and I will use the '

Code: Select all

' BBCode option in the future. thank you.
Post Reply