Edit report at http://bugs.php.net/bug.php?id=50911&edit=1

 ID:                 50911
 Updated by:         johan...@php.net
 Reported by:        T dot J dot Hunt at open dot ac dot uk
 Summary:            Can create a subclass when base class constructor is
                     private
-Status:             Open
+Status:             Bogus
 Type:               Feature/Change Request
 Package:            Class/Object related
 Operating System:   *
 PHP Version:        5.2.13RC1
 Block user comment: N
 Private report:     N

 New Comment:

This is the way the class based object model is supposed to work. You
are calling the ctor from base's scope.


Previous Comments:
------------------------------------------------------------------------
[2010-02-02 13:59:38] 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-02-02 13:49:10] der...@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] der...@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/bug.php?id=50911&edit=1

Reply via email to