From:             ahb at ahb dot net
Operating system: Windows 2000
PHP version:      5.0.1
PHP Bug Type:     Zend Engine 2 problem
Bug description:  Access to private variables from superclasses does not work correctly

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 bug report at http://bugs.php.net/?id=29674&edit=1
-- 
Try a CVS snapshot (php4):  http://bugs.php.net/fix.php?id=29674&r=trysnapshot4
Try a CVS snapshot (php5):  http://bugs.php.net/fix.php?id=29674&r=trysnapshot5
Fixed in CVS:               http://bugs.php.net/fix.php?id=29674&r=fixedcvs
Fixed in release:           http://bugs.php.net/fix.php?id=29674&r=alreadyfixed
Need backtrace:             http://bugs.php.net/fix.php?id=29674&r=needtrace
Need Reproduce Script:      http://bugs.php.net/fix.php?id=29674&r=needscript
Try newer version:          http://bugs.php.net/fix.php?id=29674&r=oldversion
Not developer issue:        http://bugs.php.net/fix.php?id=29674&r=support
Expected behavior:          http://bugs.php.net/fix.php?id=29674&r=notwrong
Not enough info:            http://bugs.php.net/fix.php?id=29674&r=notenoughinfo
Submitted twice:            http://bugs.php.net/fix.php?id=29674&r=submittedtwice
register_globals:           http://bugs.php.net/fix.php?id=29674&r=globals
PHP 3 support discontinued: http://bugs.php.net/fix.php?id=29674&r=php3
Daylight Savings:           http://bugs.php.net/fix.php?id=29674&r=dst
IIS Stability:              http://bugs.php.net/fix.php?id=29674&r=isapi
Install GNU Sed:            http://bugs.php.net/fix.php?id=29674&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=29674&r=float

Reply via email to