php.net |  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
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: barry dot verdon at complinet dot com
New email:
PHP Version: OS:

 

 [2005-08-24 18:41 UTC] barry dot verdon at complinet dot com
Description:
------------
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:
---------------
<?php
	class parentClass {
		protected function protectedFunction() {
			print("This is a protected function.");
		}
	}
	
	class childClass extends parentClass {
		public function publicFunction() {
			$childClass = new childClass();
			$childClass->protectedFunction();
		}
	}
	
	$childClass = new childClass();
	$childClass->publicFunction();
?>

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

Actual result:
--------------
This is a protected function.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

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

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-2020 The PHP Group
All rights reserved.
Last updated: Sun Sep 27 10:01:22 2020 UTC