RE: PHP5 Inheritance/method override problem

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

 



> -----Original Message-----
> From: Richard Morris [mailto:richard@xxxxxxxxxxxxx]
> Sent: Wednesday, December 06, 2006 10:22 AM
> To: php-general@xxxxxxxxxxxxx
> Subject:  PHP5 Inheritance/method override problem
> 
> 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
> 

Here you have defined $c as a ClassB object.
ClassB has all the functions and variables of ClassA, plus those of ClassB.
If you define a function in ClassB which has the same name as a function in
ClassA, it will override it.

"$this" refers to the ClassB object (even inside the ClassA function), since
you defined $c as a ClassB object.  Therefore, the function exists() will be
the ClassB function.

I would suggest you rename the ClassB exists() function in order to do what
you want to do.

Here is a good example to play with that will help you understand
inheritance a bit better.

<?php
class a
{
   function showInfo()
   {
     print "I am class " . get_class($this) . ", my parent is "
.get_parent_class($this); 
   }
}

class b extends a {}

class c extends b {}

$myInstanceB = new b();
$myInstanceB->showInfo();
print "<br />";
$myInstanceC = new c();
$myInstanceC->showInfo();

/*
output:
I am class b, my parent is a
I am class c, my parent is b
*/
?> 

Good luck and I hope that helps :)

-B

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