Re: PHP5 Inheritance/method override problem

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

 



You can't override the method signature in subclasses in PHP5.

If you want this in PHP6, research the ...discusssion... from last
month on this topic on the PHP-Internals mailing list, and weigh in if
it's not already a Dead Horse.

I recall the ...discussion... more for entertainment value than actual
content, so can't tell you what the outcome was.
:-)

On Wed, December 6, 2006 9:22 am, Richard Morris wrote:
> 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
>
>


-- 
Some people have a "gift" link here.
Know what I want?
I want you to buy a CD from some starving artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?

-- 
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