php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #60375 Accessibility between classes
Submitted: 2011-11-24 20:52 UTC Modified: 2011-11-25 04:53 UTC
From: luraschigabriel at gmail dot com Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 5.3.8 OS: Windows XP SP3
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: luraschigabriel at gmail dot com
New email:
PHP Version: OS:

 

 [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'.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-11-25 04:53 UTC] laruence@php.net
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'.
 [2011-11-25 04:53 UTC] laruence@php.net
-Status: Open +Status: Bogus
 [2011-11-25 17:20 UTC] luraschigabriel at gmail dot com
Ohhhh thanks, it's logic... Please delete this bug report.

Regards,
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Jan 15 05:01:27 2025 UTC