|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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".
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Dec 04 12:00:02 2025 UTC |
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(); ?>......... 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.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 erroras 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.