php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #52245 Private member of parent class accessible from child class.
Submitted: 2010-07-04 13:22 UTC Modified: 2010-07-28 14:25 UTC
From: phpamid at gmail dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 5.3.2 OS: Windows 7 x64
Private report: No CVE-ID: None
 [2010-07-04 13:22 UTC] phpamid at gmail dot com
Description:
------------
<?php
class Father {
	private $my_private_member = 'Father private member';
	public function show_private_member()
	{
		echo $this->my_private_member;
	}
}

class Son extends Father {
	private $my_private_member = 'Son private member';
}

//After init Son object, it has both private members, we can see it if we 
var_dump($son_instance);
$son_instance = new Son;

//Following manual php.net the output "Son private member" is expected here, but 
the result is "Father private member"
$son_instance->show_private_member();
?>

Test script:
---------------
<?php
class Father {
	private $my_private_member = 'Father private member';
	public function show_private_member()
	{
		echo $this->my_private_member;
	}
}

class Son extends Father {
	private $my_private_member = 'Son private member';
}

//After init Son object, it has both private members, we can see it if we var_dump($son_instance);
$son_instance = new Son;

//Following manual php.net the output "Son private member" is expected here, but the result is "Father private member"
$son_instance->show_private_member();
?>

Expected result:
----------------
Expected "Son private member".


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-07-04 13:50 UTC] giorgio dot liscio at email dot it
i think that is a bug but i'm not sure if is a php behavior

<?php
class Father{private $x = 'FATHER';
public function a(){echo $this->x;}
}

class Son extends Father{private $x = 'SON';
// try to decomment this:
//public function a(){echo $this->x;}
}

$son_instance = new Son;
$son_instance->a();

?>
 [2010-07-04 14:00 UTC] phpamid at gmail dot com
.........
class Son extends Father{private $x = 'SON';
// try to decomment this:
//public function a(){echo $this->x;}
}
.............

I know if I override method a() in the child - everything will be ok, but it's not necessary to override method a(), because method a() is inherited form father class and it's public.

p.s. if it's a bug, then it's php behavior.
 [2010-07-04 14:26 UTC] giorgio dot liscio at email dot it
sure i know i don't need to override the method with the same one in the new class to fix this problem

in the parent class the field is private... so theoretically i can not access the member in the derived class

class A
{
	private $a = "a";
}
class B extends A
{
        private $a = "b"; // is this an error?
}

it probably is, and maybe php should throw an error
 [2010-07-04 14:32 UTC] giorgio dot liscio at email dot it
in fact if you use protected instead of private it works as expected
but the "private" issue should be clarified, i think

* i'm sorry for the second post
 [2010-07-04 15:11 UTC] phpamid at gmail dot com
as for your code:

class A
{
	private $a = "a";
}
class B extends A
{
        private $a = "b"; // is this an error?
}

i think every class has own private member, so there is no error.
--------------------------------------------------------------------
Ok, I got you.
I hope that this issue will be clarified.
Thank you for discussion.
 [2010-07-08 13:00 UTC] johannes@php.net
-Status: Open +Status: Bogus
 [2010-07-08 13:00 UTC] johannes@php.net
You are calling show_private_member(). That method belongs to Father and therefore has access to Father's private elements. The method has no access to other class's private elements. Are you looking for protected elements?
 [2010-07-14 09:25 UTC] phpamid at gmail dot com
1) Answer for your question "Are you looking for protected elements?" is "Bug #52245 Private member of parent class accessible from child class."

2) Why you say that show_private_member() belongs to Parent class, if it is inherited by its Child? In addition, I redefined the private member in Child class - then, according to the paradigm of OOP polimoryizm, the method should return a value of Child private member, otherwise all you have said is contrary to OOP, in particular, to polymorphism.
 [2010-07-27 09:43 UTC] phpamid at gmail dot com
-Status: Bogus +Status: Open
 [2010-07-27 09:43 UTC] phpamid at gmail dot com
Could you please answer to my questions?
 [2010-07-28 14:23 UTC] johannes@php.net
Private enforces encapsulation, a key element of OOP. The current behavior ensures you can extend the class without an effect on  show_private_member() and whatever happens in there. With your change extending the class can hae an effect (by accident) on the method.
 [2010-07-28 14:25 UTC] johannes@php.net
-Status: Open +Status: Bogus
 [2010-07-28 14:25 UTC] johannes@php.net
.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Tue Apr 01 22:01:29 2025 UTC