|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #34237 A protected method should cause a fatal error if called publicly.
Submitted: 2005-08-24 18:41 UTC Modified: 2005-08-24 20:58 UTC
From: barry dot verdon at complinet dot com Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 5.* OS: *
Private report: No CVE-ID: None
 [2005-08-24 18:41 UTC] barry dot verdon at complinet dot com
A protected function is accessible publicly if a child class has a public function that instantiates the child object and calls the protected function. This public function can then be called publicly. I would have expected a fatal error to be produced from the protected function call.

Is this intended behaviour? Or should I declare the function as public in the case of this not being the desired functionality and may change at a later point?

Reproduce code:
	class parentClass {
		protected function protectedFunction() {
			print("This is a protected function.");
	class childClass extends parentClass {
		public function publicFunction() {
			$childClass = new childClass();
	$childClass = new childClass();

Expected result:
Fatal Error on line 11, $childClass->protectedFunction().

Actual result:
This is a protected function.


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2005-08-24 20:58 UTC]
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at and the instructions on how to report
a bug at

In that scope you're allowed to call protected member functions. Since it is a compatible scope. In other words visibility works scope/class wise not object wise.
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Jun 22 15:01:28 2024 UTC