Page 1 of 1

problems with Singleton and Visibilty?

Posted: Wed Jul 11, 2007 5:05 pm
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]

Posted: Wed Jul 11, 2007 5:13 pm
by feyd
$db should be $this->db in the constructor.

Posted: Wed Jul 11, 2007 6:29 pm
by joncampbell
That did the trick, thank you... and I will use the '

Code: Select all

' BBCode option in the future. thank you.