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
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
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

Add a Patch

Pull Requests

Add a Pull Request

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-2024 The PHP Group
All rights reserved.
Last updated: Thu May 23 03:01:28 2024 UTC