From:             T dot J dot Hunt at open dot ac dot uk
Operating system: Windows and Linux
PHP version:      5.2.13RC1
PHP Bug Type:     Class/Object related
Bug description:  Can create a subclass when base class constructor is private

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 bug report at http://bugs.php.net/?id=50911&edit=1
-- 
Try a snapshot (PHP 5.2):            
http://bugs.php.net/fix.php?id=50911&r=trysnapshot52
Try a snapshot (PHP 5.3):            
http://bugs.php.net/fix.php?id=50911&r=trysnapshot53
Try a snapshot (PHP 6.0):            
http://bugs.php.net/fix.php?id=50911&r=trysnapshot60
Fixed in SVN:                        
http://bugs.php.net/fix.php?id=50911&r=fixed
Fixed in SVN and need be documented: 
http://bugs.php.net/fix.php?id=50911&r=needdocs
Fixed in release:                    
http://bugs.php.net/fix.php?id=50911&r=alreadyfixed
Need backtrace:                      
http://bugs.php.net/fix.php?id=50911&r=needtrace
Need Reproduce Script:               
http://bugs.php.net/fix.php?id=50911&r=needscript
Try newer version:                   
http://bugs.php.net/fix.php?id=50911&r=oldversion
Not developer issue:                 
http://bugs.php.net/fix.php?id=50911&r=support
Expected behavior:                   
http://bugs.php.net/fix.php?id=50911&r=notwrong
Not enough info:                     
http://bugs.php.net/fix.php?id=50911&r=notenoughinfo
Submitted twice:                     
http://bugs.php.net/fix.php?id=50911&r=submittedtwice
register_globals:                    
http://bugs.php.net/fix.php?id=50911&r=globals
PHP 4 support discontinued:          http://bugs.php.net/fix.php?id=50911&r=php4
Daylight Savings:                    http://bugs.php.net/fix.php?id=50911&r=dst
IIS Stability:                       
http://bugs.php.net/fix.php?id=50911&r=isapi
Install GNU Sed:                     
http://bugs.php.net/fix.php?id=50911&r=gnused
Floating point limitations:          
http://bugs.php.net/fix.php?id=50911&r=float
No Zend Extensions:                  
http://bugs.php.net/fix.php?id=50911&r=nozend
MySQL Configuration Error:           
http://bugs.php.net/fix.php?id=50911&r=mysqlcfg

Reply via email to