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:4
Avg. Score:3.5 ± 1.7
Reproduced:3 of 3 (100.0%)
Same Version:1 (33.3%)
Same OS:2 (66.7%)
From: vrana@php.net Assigned:
Status: Verified Package: Scripting Engine problem
PHP Version: 5.3.2 OS: *
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
MUST BE VALID
Solve the problem:
47 - 41 = ?
Subscribe to this entry?

 
 [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-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 12:01:27 2024 UTC