|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2011-11-24 20:52 UTC] luraschigabriel at gmail dot com
Description:
------------
When a construct is not declared on a inherited class and it's declared on base
class as protected, an instantiation of the derived class inside another class,
causes a fatal accessibility error.
Test script:
---------------
abstract class A {
protected function __construct() {
echo "A constructor\n";
}
}
class B extends A {
/* NO __construct() declared */
public function foo() {
echo "foo method";
}
}
class C {
public $B;
function __construct() {
$this->B = new B(); // Produces an error because B has no construct declared.
}
}
$c = new C();
Expected result:
----------------
This behavior is not documented.
Actual result:
--------------
Error: document.php line 18 - Call to protected A::__construct() from context 'C'.
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Tue Oct 28 14:00:01 2025 UTC |
you are attemptting to call a protected constructor from a outside scope. just like: abstract class A { protected function __construct() { echo "A constructor\n"; } } class B extends A { /* NO __construct() declared */ public function foo() { echo "foo method"; } } new B();//PHP Fatal error: Call to protected A::__construct() from invalid context if you want B have a non-protected constructor, you must redeclared it with 'public'.