Bug #18024: no shadowing of parent variables
| From: | adresse at brutele dot be | Date: | Thu, 27 Jun 2002 20:26:57 +0000 |
| Subject: | Bug #18024: no shadowing of parent variables | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-12262@lists.php.net to get a copy of this message | ||
From: adresse@brutele.be
Operating system: Windows XP home
PHP version: 4.2.0
PHP Bug Type: Class/Object related
Bug description: no shadowing of parent variables
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 bug report at http://bugs.php.net/?id=18024&edit=1
--
Fixed in CVS: http://bugs.php.net/fix.php?id=18024&r=fixedcvs
Fixed in release: http://bugs.php.net/fix.php?id=18024&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=18024&r=needtrace
Try newer version: http://bugs.php.net/fix.php?id=18024&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=18024&r=support
Expected behavior: http://bugs.php.net/fix.php?id=18024&r=notwrong
Not enough info: http://bugs.php.net/fix.php?id=18024&r=notenoughinfo
Submitted twice: http://bugs.php.net/fix.php?id=18024&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=18024&r=globals