RE: [PHP] PHP5 Inheritance/method override problem
| From: | Edward Kay | Date: | Wed, 06 Dec 2006 16:59:17 +0000 |
| Subject: | RE: [PHP] PHP5 Inheritance/method override problem | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-245574@lists.php.net to get a copy of this message | ||
> > Hi Richard,
> >
> > This is very strange. I can certainly reproduce the problem. Putting a
> > var_dump($this) in ClassA's save method shows that PHP sees $this as an
> > instance of ClassB - when it should be of ClassA.
>
> what *you* think it should be and what php's developers decided
> it should be
> about 3+ years ago obviously differ.
>
> if you have an instance of ClassB it is exactly that - refering to $this
> in the parent class definition won't magically make the class of
> the instance change.
>
> $this is plain this and not something else.
Yes, you are indeed correct.
I was thinking that as B extends A, A wouldn't know anything about B thus
$this in A would refer to A. I can now see that my logic was flawed here.
Thanks for pointing it out Jochem!
Richard, replacing your
if ($this->exists($value))
with
if (self::exists($value))
will call A's exists method, as you require. As Brad pointed out however,
your defintion of B overrides A's exists method. If you need both, then you
should rename the method in class B.
Edward