php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #18024 no shadowing of parent variables
Submitted: 2002-06-27 16:26 UTC Modified: 2002-09-26 19:50 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: adresse at brutele dot be Assigned:
Status: No Feedback Package: Scripting Engine problem
PHP Version: 4.2.0 OS: Windows XP home
Private report: No CVE-ID: None
 [2002-06-27 16:26 UTC] adresse at brutele dot 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

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-09-04 03:38 UTC] antonio dot afonso at link dot pt
I have the same problem and i'm using php 4.0.6 on linux
 [2002-09-05 16:12 UTC] jmcastagnetto@php.net
First, PHP is not an OOP language, it is a scripting language that supports OOP-style coding.

Second, as the type of variables or their existence is dynamic (i.e. no need to declare a variable or its type before using it), and as there is no concept of private/public/protected/etc. then it is the expected behaviour that the subclass overrides the parent's if you use it directly, similar behaviour happens if you override a parent's method.

Third, there are no namespaces as such in PHP (need I elaborate more?)

BTW, you can still refer to the parent's members using the parent keyword, e.g. parent::foo(), in other words you should refer to the var/method explicitely in the code.

If you have some code contribution that can implement the behaviour you are used in C++ or other OOP language, feel free to submit it to the php-dev list. It will be good if you look at the archives and read the discussions on the OOP topics that have happened before there too.

 [2002-09-26 19:50 UTC] sniper@php.net
No feedback was provided. The bug is being suspended because
we assume that you are no longer experiencing the problem.
If this is not the case and you are able to provide the
information that was requested earlier, please do so and
change the status of the bug back to "Open". Thank you.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 14:01:32 2024 UTC