RE: overloading variables in child classes
| From: | Andrew Kirilenko | Date: | Sat, 27 Oct 2001 20:17:09 +0000 |
| Subject: | RE: overloading variables in child classes | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-72585@lists.php.net to get a copy of this message | ||
Hello!
> Is there something I don't understand ?
Yes.
According OOP concepts, derived class inherits all functionality and data
from the base. So, if you have $shared variable in the base class, your
derived class will have it automatically. But PHP allows you to declare this
variable once again in the derived class. In C++ you will get comilation
error in this case.
Best regards,
Andrew Kirilenko.
> -----Original Message-----
> From: Alain Dresse [mailto:adresse@brutele.be]
> Sent: Saturday, October 27, 2001 10:12 PM
> To: php-general@lists.php.net
> Subject: [PHP] overloading variables in child classes
>
>
> Hi all,
>
> I am a bit puzzled by the way PHP treates variables that have the
> same name
> in a base class and a child class.
> It seems to me that if a child class and its parent both have a variable
> with the same name, then they should be different variables. Instead, the
> example below indicates that they is only one variable :
> ----
> class base {
> var $shared;
> function base() {$this->shared = "base";}
> function test() {echo("\$shared in base : " . $this->shared .
> "<br>\n");}
> }
>
> class child extends base {
> var $shared;
> function child(){$this->base(); $this->shared = "child";}
> function test() {
> echo ("\$shared in child : " . $this->shared . "<br>\n");
> parent::test();
> }
> }
>
> $the_child = new child();
> $the_child->test();
> ----
>
> I would have expected the output to be
> $shared in child : child
> $shared in base : base
>
> instead I have
> $shared in child : child
> $shared in base : child
>
> Is there something I don't understand ?
>
> Regards,
> Alain Dresse
> adresse@brutele.be