#29674 [Fbk->NoF]: Access to private variables from superclasses does not work correctly
| From: | php-bugs at lists dot php dot net | Date: | Mon, 14 Mar 2005 00:00:25 +0000 |
| Subject: | #29674 [Fbk->NoF]: Access to private variables from superclasses does not work correctly | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-75292@lists.php.net to get a copy of this message | ||
ID: 29674
Updated by: php-bugs@lists.php.net
Reported By: ahb at ahb dot net
-Status: Feedback
+Status: No Feedback
Bug Type: Zend Engine 2 problem
Operating System: Windows 2000
PHP Version: 5.0.1
New Comment:
No feedback was provided for this bug for over a week, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
Previous Comments:
------------------------------------------------------------------------
[2005-03-06 20:55:49] sniper@php.net
Please try using this CVS snapshot:
http://snaps.php.net/php5-latest.tar.gz
For Windows:
http://snaps.php.net/win32/php5-win32-latest.zip
------------------------------------------------------------------------
[2004-08-29 06:52:12] tony2001@php.net
Here is quick fix for this bug:
http://tony2001.phpclub.net/dev/tmp/bug29674.diff
and test for it:
http://tony2001.phpclub.net/dev/tmp/bug29674.phpt
------------------------------------------------------------------------
[2004-08-14 13:08:30] ahb at ahb dot net
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 ();
?>
------------------------------------------------------------------------
[2004-08-14 12:53:44] helly@php.net
Would you mind cut the script sown to the part that doesn't work? I
don't want to inspect thousands of lines which are not related to your
'error'.
------------------------------------------------------------------------
[2004-08-14 12:34:43] 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 ---
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=29674&edit=1