php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #50911 Can create a subclass when base class constructor is private
Submitted: 2010-02-02 13:30 UTC Modified: 2010-12-22 15:39 UTC
From: T dot J dot Hunt at open dot ac dot uk Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 5.2.13RC1 OS: *
Private report: No CVE-ID: None
 [2010-02-02 13:30 UTC] 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.)


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-02-02 13:36 UTC] 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:43 UTC] 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:49 UTC] 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:59 UTC] T dot J dot Hunt at open dot ac dot uk
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
 [2010-12-03 17:45 UTC] jani@php.net
-Package: Feature/Change Request +Package: Class/Object related
 [2010-12-22 15:39 UTC] johannes@php.net
-Status: Open +Status: Bogus
 [2010-12-22 15:39 UTC] johannes@php.net
This is the way the class based object model is supposed to work. You are calling the ctor from base's scope.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 12:01:27 2024 UTC