Call to a member function on a non-object (PHP 5)

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

User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

You don't need to final the getInstance method, though you might want to final the constructor and make it private:

Code: Select all

<?php
class Database {
    private $instance = null;

    final private function __construct() {}

    public static function getInstance() {
        if (null === self::$instance) {
            self::$instance = new self();
        }

        return self::$instance;
    }

    /* ... the rest of the class code ... */
}
?>
Post Reply