|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2004-08-14 12:34 UTC] ahb at ahb dot net
Description:
------------
In the code below you can see a class A defining two methods. The "showvarnames" function will show all variable names that are defined in a class and the "showvarvalues" function will show all variable values.
If I extend the class A by class B, instantiate a object of class B and call the "showvarvalues" function on this Object, I will get a "Cannot access private property B::$b_private" Error.
starting the script below will give the following output :
--- cut here ---
Variable names
b_private
b_protected
b_public
a_protected
a_public
Variable values
Fatal error: Cannot access private property B::$b_private in bug.php(32) : eval()'d code on line 1
--- cut here ---
So as you can see the "showvarnames" function shows the correct variable names (a_private is not accessible form B), but I cannot access the variable b_private.
Reproduce code:
---------------
<?
class A
{
private $a_private;
protected $a_protected;
public $a_public;
function __construct()
{
$a_private = "private_a";
$a_protected = "protected_a";
$a_public = "public_a";
}
function showvarnames ()
{
$arrv = get_class_vars(get_class ($this));
while (list($name, $value) = each ($arrv))
{
echo $name."<br>";
}
}
function showvarvalues ()
{
$arrv = get_class_vars(get_class ($this));
while (list($name, $value) = each ($arrv))
{
eval ("\$val = \$this->".$name.";");
echo $name." => ".$val."<br>";
}
}
}
class B extends A
{
private $b_private;
protected $b_protected;
public $b_public;
function __construct ()
{
$b_private = "private_b";
$b_protected = "protected_b";
$b_public = "public_b";
}
}
$obj = new B ();
echo "Variable names<br>";
$obj->showvarnames ();
echo "<br>";
echo "Variable values<br>";
$obj->showvarvalues ();
?>
Expected result:
----------------
I would expect that I can access the b_private variable :-)
A similar behaviour is described in bug #26350, but I think the behaviour is not what I would expect from a OO language.
Even if this works as designed, this is different from other OO languages like Java, C++ and will make the implementation of several algorithms useless, or will force the user not to use private members.
Actual result:
--------------
--- cut here ---
Variable names
b_private
b_protected
b_public
a_protected
a_public
Variable values
Fatal error: Cannot access private property B::$b_private in bug.php(32) : eval()'d code on line 1
--- cut here ---
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Nov 05 05:00:01 2025 UTC |
Ok, I reduced the script to the important stuff : <? //--- Base class ------------------------------------------ class A { private $a_private = "private_a"; // private variable //--- Function in Base class ---------------------------- function showvarvalues () { $arrv = get_class_vars(get_class ($this)); while (list($name, $value) = each ($arrv)) { echo $name." => ".$this->$name."<br>"; } } } class B extends A { private $b_private = "private_b"; // private variable } //--- Call inherited function ----------------------------- $obj = new B (); $obj->showvarvalues (); ?>