Edit report at http://bugs.php.net/bug.php?id=52245&edit=1
ID: 52245
Comment by: giorgio dot liscio at email dot it
Reported by: phpamid at gmail dot com
Summary: Private member of parent class accessible from child
class.
Status: Open
Type: Bug
Package: *General Issues
Operating System: Windows 7 x64
PHP Version: 5.3.2
New Comment:
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
Previous Comments:
------------------------------------------------------------------------
[2010-07-04 14:26:04] 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:00:51] 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 13:50:26] 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 13:22:18] 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".
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/bug.php?id=52245&edit=1