#18024 [Com]: no shadowing of parent variables
| From: | antonio dot afonso at link dot pt | Date: | Wed, 04 Sep 2002 08:38:49 +0000 |
| Subject: | #18024 [Com]: no shadowing of parent variables | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-18394@lists.php.net to get a copy of this message | ||
ID: 18024
Comment by: antonio.afonso@link.pt
Reported By: adresse@brutele.be
Status: Open
Bug Type: Class/Object related
Operating System: Windows XP home
PHP Version: 4.2.0
New Comment:
I have the same problem and i'm using php 4.0.6 on linux
Previous Comments:
------------------------------------------------------------------------
[2002-06-27 16:26:56] adresse@brutele.be
I am surprised that a subclass can redeclare parent class variables. It
seems to me that this could hinder reusability, as a programmer must
know all variables declared in the parent class (possibly coded by
another programmer) to avoid mistakes.
Consider the following code:
---code---
class Father {
var $variable = "father";
function parentVar() {return $this->variable;}
}
class Child extends Father {
var $variable = "child";
function childVar() {return $this->variable;}
}
$testChild = new Child();
echo "Expect child : ". $testChild->childVar() . "\n";
echo "Expect father : ". $testChild->parentVar() . "\n";
---endcode---
In java or C++, Father::variable and Child::variable would be different
variables;
References to $this->variable would refer to Child::variable in Child
and to Father::variable in Father. The redefinition would not break any
functions written in Father. Member functions in Child could access
Father::variable by specifying it.
The PHP documentation hints that this was considered. Indeed, in the
documentation for :: one can read
"Sometimes it is useful to refer to functions and
variables in base classes or to refer to functions in
classes that have not yet any instances. The ::
operator is being used for this."
However, the actual behaviour is PHP is that the Child class overwrites
the variable from the Father, as can be seen by running the script. The
results are :
Expect child : child
Expect father : child
Am I overseeing something ?
Thanks for your attention,
Alain Dresse
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=18024&edit=1