php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #29674 Access to private variables from superclasses does not work correctly
Submitted: 2004-08-14 12:34 UTC Modified: 2005-03-14 01:00 UTC
Votes:3
Avg. Score:5.0 ± 0.0
Reproduced:2 of 2 (100.0%)
Same Version:1 (50.0%)
Same OS:1 (50.0%)
From: ahb at ahb dot net Assigned:
Status: No Feedback Package: Scripting Engine problem
PHP Version: 5.0.1 OS: Windows 2000
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [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 ---


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-08-14 12:53 UTC] 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 13:08 UTC] 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 ();

?>
 [2005-03-06 20:55 UTC] 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


 [2005-03-14 01:00 UTC] php-bugs at lists dot php dot net
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".
 
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Sun Sep 25 00:05:53 2022 UTC