ID: 50911 Updated by: der...@php.net Reported By: T dot J dot Hunt at open dot ac dot uk -Status: Open +Status: Bogus Bug Type: Class/Object related Operating System: Windows and Linux PHP Version: 5.2.13RC1 New Comment:
Thank you for taking the time to write to us, but this is not a bug. Please double-check the documentation available at http://www.php.net/manual/ and the instructions on how to report a bug at http://bugs.php.net/how-to-report.php This is not a bug. privates are not inherited (an inherted class doesn't know about private properties/methods in the base class). This leaves "sub" with an empty constructor. So what happens is that base::init() can be called, which constructs "sub" with the default empty constructor. I don't see why this shouldn't work. Previous Comments: ------------------------------------------------------------------------ [2010-02-02 13:30:42] T dot J dot Hunt at open dot ac dot uk Description: ------------ If the base class has a private constructor, and the subclass does not explicitly define a constructor, then you can create an instance of the subclass from a static method in the base class. This is similar to, but different from bug 38064 and bug 44141. The behaviour is the same in PHP 5.2.11. This bug does not seem to be present in PHP 5.2.4, judging by the report of one of my users: http://moodle.org/mod/forum/discuss.php?d=142675#p623807 Reproduce code: --------------- class base { private function __construct() { debug_print_backtrace(); // Not essential. } public static function init() { return new sub(); } } class sub extends base { } $test = base::init(); echo "It worked! (It should have failed.)\n"; Expected result: ---------------- I would expect to see an error message like: Fatal error: Call to private sub::__construct() from context 'base' in /fs2/www/html/tjh238/moodle_git/test.php on line 8 That is the error you get if you change the definition of sub to: class sub extends base { private function __construct() { parent::__construct(); } } Actual result: -------------- #0 base1->__construct() called at [/fs2/www/html/tjh238/moodle_git/test.php:8] #1 base1::init() called at [/fs2/www/html/tjh238/moodle_git/test.php:14] It worked! (It should have failed.) ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=50911&edit=1