RE: PHP5 Inheritance/method override problem

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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.

I'm afraid it's stumped me too! Does anyone else have any thoughts?

Edward


> Hi,
>
> I came across this problem and I was wondering if someone might be able
> to explain to my why it is happening.  I put together a short example to
>   illustrate the problem that I am having.
>
> Basically I have one class that performs a certain task.  I then have a
> second class which extends the first class and performs some tasks in
> addition to the first class.  Even though ClassA should know nothing
> about ClassB since ClassA is the parent, ClassA actually calls ClassB's
> exist method when it should be calling it's own.
>
> I know that description is really wordy and probably confusing which is
> why I provided below the example code that illustrates the problem.
>
> Doesn't this break the idea of inheritance?  I can get it to work if I
> change ClassB::exists to some other name such as ClassB::exists2.
>
> If anyone can enlighten me as to why this problem is occurring or
> suggest a fix I would really appreciate it.  Thank you.
>
>
>
> expected output
> ---------------
> ClassB - save
> ClassA - save
> ClassA - exists
> ClassA - update
>
>
>
> actual output
> -------------
> ClassB - save
> ClassA - save
>
>
> Warning:  Missing argument 2 for ClassB::exists(), called in
> /website/overridetest.php on line 6 and defined in
> /website/overridetest.php on line 33
>
> ClassB - exists
> ClassA - update
>
>
>
> overridetest.php
> ----------------
> <?php
>    class ClassA {
>      public function save($value) {
>        echo "ClassA - save\n";
>
>        if ($this->exists($value))
>          $this->update($value);
>        else
>          $this->insert($value);
>      }
>
>      protected function insert($value) {
>        echo "ClassA - insert\n";
>      }
>
>      protected function update($value) {
>        echo "ClassA - update\n";
>      }
>
>      public function exists($value) {
>        echo "ClassA - exists\n";
>        return true;
>      }
>    }
>
>    class ClassB extends ClassA {
>      public function save($value, $classAValue) {
>        echo "ClassB - save\n";
>          parent::save($classAValue);
>          // do some additional work...
>      }
>
>      public function exists($value, $classAValue) {
>          // does a different search than ClassA
>        echo "ClassB - exists\n";
>          return true;
>      }
>    }
>
>    $c = new ClassB();
>    $c->save('test', 'test2');
> ?>
>
> --
> Richard Morris
> HDD Broker, Inc.
>
>      Toll-Free: +1 866 960-3331
> International: +1 250 474-6022
>            Fax: +1 250 474-6099
>         E-Mail: richard@xxxxxxxxxxxxx
>       Web Site: www.hddbroker.com
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
>

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux