php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #52120 Unable to call inherited protected __construct
Submitted: 2010-06-18 17:15 UTC Modified: 2019-08-15 13:45 UTC
Votes:3
Avg. Score:4.3 ± 0.9
Reproduced:2 of 2 (100.0%)
Same Version:0 (0.0%)
Same OS:1 (50.0%)
From: vrana@php.net Assigned:
Status: Verified Package: Scripting Engine problem
PHP Version: 5.3.2 OS: *
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2010-06-18 17:15 UTC] vrana@php.net
Description:
------------
Protected visibility allows accessing parent members and also members of parent's children. If class A defines protected method f() and classes B and C are children of A then B can call C::f(). This is true for all methods except __construct().

Test script:
---------------
class A {
	protected function __construct() {
	}
}

class B extends A {
	static function test() {
		new C;
	}
}

class C extends A {
	protected function __construct() {
		echo "OK\n";
	}
}

B::test();


Expected result:
----------------
OK

Actual result:
--------------
Fatal error: Call to protected C::__construct() from context 'B' on line 9

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-06-20 00:39 UTC] felipe@php.net
-Status: Open +Status: Feedback -Package: Class/Object related +Package: Scripting Engine problem
 [2010-06-20 00:39 UTC] felipe@php.net
"This is true for all methods except __construct()."
Can you provide an test case for non __construct method? I cannot reproduce it.
 [2010-06-21 14:21 UTC] vrana@php.net
-Status: Feedback +Status: Open
 [2010-06-21 14:21 UTC] vrana@php.net
<?php
class A {
	protected function f() {
	}
}

class B extends A {
	static function test() {
		$c = new C;
		$c->f();
	}
}

class C extends A {
	protected function f() {
		echo "OK\n";
	}
}

B::test();
?>

This should output OK which it really does.
 [2010-07-03 18:31 UTC] felipe@php.net
There was an issue related to this question times ago. See bug #37632
Which has a test (Zend/tests/bug37632.phpt) checking exactly what you are reporting as a bug. The fix for this issue is simple, but I don't know really if this is the expected behavior or not.
 [2019-08-15 13:45 UTC] nikic@php.net
-Status: Open +Status: Verified
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Fri Dec 13 05:01:26 2019 UTC