private/public __construct in PHP / Singleton pattern

From: Date: Sun, 05 Jun 2005 20:11:01 +0000
Subject: private/public __construct in PHP / Singleton pattern
Groups: php.pear.dev 
Request: Send a blank email to pear-dev+get-37982@lists.php.net to get a copy of this message
I've ran across what I feel is a bug in PHP5's OOP. I'm trying to create a singleton pattern, but PHP5 doesn't allow you to do this with base classes with public __construct() it appears. For instance: <?php abstract class A {
    public function __construct()
    {
        echo __CLASS__;
    }
} class B extends A {
    private static $instance;
    private function __construct()
    {
        parent::__construct();
    }
    public static function singleton()
    {
        if (!isset(self::$instance)) {
            $c = __CLASS__;
            self::$instance = new $c;
        }
        return self::$instance;
    }
} $b = B::singleton(); ?> Results in the following error: <br /> <b>Fatal error</b>: Access level to B::__construct() must be public (as in class A) in <b>/usr/local/httpd/htdocs/framework/foo.php</b> on line <b>29</b><br /> Am the the only one that finds this retarded? --Joe -- Joe Stump http://www.joestump.net

« previous php.pear.dev (#37982) next »