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

Add a Patch

Pull Requests

Add a Pull Request

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-2024 The PHP Group
All rights reserved.
Last updated: Mon Jul 22 09:01:28 2024 UTC