#50911 [Opn]: Can create a subclass when base class constructor is private
| From: | T dot J dot Hunt at open dot ac dot uk | Date: | Tue, 02 Feb 2010 13:59:38 +0000 |
| Subject: | #50911 [Opn]: Can create a subclass when base class constructor is private | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-147154@lists.php.net to get a copy of this message | ||
ID: 50911
User updated by: T dot J dot Hunt at open dot ac dot uk
Reported By: T dot J dot Hunt at open dot ac dot uk
Status: Open
Bug Type: Class/Object related
Operating System: Windows and Linux
PHP Version: 5.2.13RC1
New Comment:
Out of interest, I just tried the equivalent Java code, since PHP OOP
is modelled on Java - at least that is my understanding. Java (I am
using the Eclipse IDE) gives the following compile error:
Implicit super constructor Base() is not visible for default
constructor. Must define an explicit constructor
Previous Comments:
------------------------------------------------------------------------
[2010-02-02 13:49:10] derick@php.net
Hmm, that is odd indeedâI think no constructor should've been called
here but "sub" be created.
------------------------------------------------------------------------
[2010-02-02 13:43:43] T dot J dot Hunt at open dot ac dot uk
But, the base constructor is called. The stack trace from the
debug_print_backtrace() call in the base constructor is output.
------------------------------------------------------------------------
[2010-02-02 13:36:05] derick@php.net
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.
------------------------------------------------------------------------
[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