From:             panza at zs dot lviv dot ua
Operating system: FreeBSD
PHP version:      5.1.4
PHP Bug Type:     Class/Object related
Bug description:  private members problem after inheritance...

Description:
------------
I have searched Bugs throughout, and found similar problems,  addressing
the visibility issue, this is quite different...

During inheritance, the private members of the parent class are inheriated
as well, but can only be modified by the methods defined in parent class.
The methods defined in the child class can not modify these variables, but
only child's own members. The problem here is, that theoretically this
situation seems wrong. Once inherited, the methods should "belong" to the
child, therefore even though they are defined in parent, they should
modify child's members when called, No? Also in this situation, as shown
in the code during var_dump of the $b variable, we can see two private
"_vars" members, how can this be logical, two variables with same name
belong to same namespace,yet one only accessible by it's parents
methods,while other by it's own methods.

Note: I first came across this problem, while using Zend Framework and
extending Zend_View class. If needed I can send source to show the
importance of this.

In my opinion, the "old" parents private members should not be shown using
var_dump and similar output functions to stop confussion, and the inherited
methods should work with members in the child class, not parent, since they
were called from child class and the logic would suguest this.

Reproduce code:
---------------
http://www.zs.lviv.ua/~panza/error.phps

Expected result:
----------------
$this belongs to B
$this belongs to B
Variable, $b->somevar, was assigned in B costructor, but the call was made
to __set in parent class, A, making it defined in _vars which belongs to A.
During retrive, we __get from parent is called, so we get the value we
assigned
string(6) "value1"
Although, this time retriving the same variable using function defined in
child class, B, we get NULL.
string(6) "value1"
This is because when inheriting a class, the private variables are
inherited as well, but only access by function which were defined in the
parent class. The functions that were defined in child class modify/access
their own variable with the same name.

object(B)#1 (3) {
  ["_glovalvars:private"]=>
  NULL
  ["_vars:private"]=>
  array(3) {
    ["somevar"]=>
    string(6) "value1"
    ["UsingfunctionFromA"]=>
    string(14) "Set _vars in A"
    ["UsingB"]=>
    string(15) "Sets _vars in B"
  }
}

Actual result:
--------------
$this belongs to B
$this belongs to B
Variable, $b->somevar, was assigned in B costructor, but the call was made
to __set in parent class, A, making it defined in _vars which belongs to A.
During retrive, we __get from parent is called, so we get the value we
assigned
string(6) "value1"
Although, this time retriving the same variable using function defined in
child class, B, we get NULL.
NULL
This is because when inheriting a class, the private variables are
inherited as well, but only access by function which were defined in the
parent class. The functions that were defined in child class modify/access
their own variable with the same name.

object(B)#1 (3) {
  ["_glovalvars:private"]=>
  NULL
  ["_vars:private"]=>
  array(1) {
    ["UsingB"]=>
    string(15) "Sets _vars in B"
  }
  ["_vars:private"]=>
  array(2) {
    ["somevar"]=>
    string(6) "value1"
    ["UsingfunctionFromA"]=>
    string(14) "Set _vars in A"
  }
}

-- 
Edit bug report at http://bugs.php.net/?id=38037&edit=1
-- 
Try a CVS snapshot (PHP 4.4): 
http://bugs.php.net/fix.php?id=38037&r=trysnapshot44
Try a CVS snapshot (PHP 5.2): 
http://bugs.php.net/fix.php?id=38037&r=trysnapshot52
Try a CVS snapshot (PHP 6.0): 
http://bugs.php.net/fix.php?id=38037&r=trysnapshot60
Fixed in CVS:                 http://bugs.php.net/fix.php?id=38037&r=fixedcvs
Fixed in release:             
http://bugs.php.net/fix.php?id=38037&r=alreadyfixed
Need backtrace:               http://bugs.php.net/fix.php?id=38037&r=needtrace
Need Reproduce Script:        http://bugs.php.net/fix.php?id=38037&r=needscript
Try newer version:            http://bugs.php.net/fix.php?id=38037&r=oldversion
Not developer issue:          http://bugs.php.net/fix.php?id=38037&r=support
Expected behavior:            http://bugs.php.net/fix.php?id=38037&r=notwrong
Not enough info:              
http://bugs.php.net/fix.php?id=38037&r=notenoughinfo
Submitted twice:              
http://bugs.php.net/fix.php?id=38037&r=submittedtwice
register_globals:             http://bugs.php.net/fix.php?id=38037&r=globals
PHP 3 support discontinued:   http://bugs.php.net/fix.php?id=38037&r=php3
Daylight Savings:             http://bugs.php.net/fix.php?id=38037&r=dst
IIS Stability:                http://bugs.php.net/fix.php?id=38037&r=isapi
Install GNU Sed:              http://bugs.php.net/fix.php?id=38037&r=gnused
Floating point limitations:   http://bugs.php.net/fix.php?id=38037&r=float
No Zend Extensions:           http://bugs.php.net/fix.php?id=38037&r=nozend
MySQL Configuration Error:    http://bugs.php.net/fix.php?id=38037&r=mysqlcfg

Reply via email to