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
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
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-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 25 20:01:45 2024 UTC