php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #26350 Inherited methods can't access private fields
Submitted: 2003-11-21 09:29 UTC Modified: 2003-12-31 12:00 UTC
From: forseti at oak dot rpg dot pl Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5CVS-2003-11-29 OS: *
Private report: No CVE-ID: None
 [2003-11-21 09:29 UTC] forseti at oak dot rpg dot pl
Description:
------------
When a method is inherited from a base class it seems that it retains its context. When it's called from derived class object, it turns out that method cannot access private fields of it's own (derived class) object.

What's strange, the inherited method can access protected fields easily. As if 'protected' opened access not only for derived classes but for base classes too.

This is a key issue if one wants to code the accessor only once, in base class so that any derived class could use it and provide read-only access to any field.

Reproduce code:
---------------
<?php 
class ParentClass {
	protected $a = 'ParentClass<br>';
	public function get($arg) {
		return $this->$arg;
	}
}

class ChildClass extends ParentClass{
	protected $b = 'ChildClass - Protected<br>';
	private $c = 'ChildClass - Private<br>';
	
}

$test = new ChildClass;
echo $test->get('a');
echo $test->get('c'); //fatal error
echo $test->get('b'); //strange - it's ok
?>

Expected result:
----------------
ParentClass
ChildClass - Protected
ChildClass - Private

Anyway, 'protected' field should be accessible only in derived classes and where declared.

Actual result:
--------------
ParentClass
ChildClass - Protected

Fatal error: Cannot access private property childclass::$c

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-12-31 12:00 UTC] andi@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

Hi,

This is the way it should be. A method can only see private members/methods of its own class and not any derived classes.

Andi
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Dec 22 05:01:30 2024 UTC