php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #42830 Implements Singleton with inheritance not possible
Submitted: 2007-10-02 13:03 UTC Modified: 2007-10-02 14:14 UTC
From: raragao at raragao dot eng dot br Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 5.2.4 OS: Windows XP / Debian
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: raragao at raragao dot eng dot br
New email:
PHP Version: OS:

 

 [2007-10-02 13:03 UTC] raragao at raragao dot eng dot br
Description:
------------
I can't implements a Singleton Pattern with inheritance.

Reproduce code:
---------------
abstract class Super {
	public function __construct () {}
}

class Sub extends Super {
	private static $object;
	private function __construct () {}
	
	public static function getInstance() {
		if (!(self::$object instanceof Sub)) {
			self::$object = new Sub();
		}
		return self::$object;
	}
}
$a = Sub::getInstance();


Expected result:
----------------
That worked.


Actual result:
--------------
Fatal error: Access level to Sub::__construct() must be public (as in class Super) in E:\sources\curso\teste\ClasseA.php on line 16

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-10-02 14:14 UTC] tony2001@php.net
abstract class Super {
	public function __construct () {}
}

=>

abstract class Super {
	private function __construct () {}
}
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sat Jul 12 20:01:32 2025 UTC