Re: PHP5 Inheritance/method override problem

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

 



# richard@xxxxxxxxxxxxx / 2006-12-06 07:22:22 -0800:
> 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.

All non-private methods are "virtual" in PHP. Besides, this will work as well:

class A
{
    function f()
    {
        $this->g();
    }
}
class B extends A
{
    function g() {}
}
$b = new B; $b->f();

As well as this:

class C extends A
{
    function extension() {}
}
function f(A $a)
{
    $a->extension();
}
 
> 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

If I change the update() methods' accesses to "private", I get the
output you expect:

roman@dagan ~ 1001:0 > php tmp/scratch04
ClassB - save
ClassA - save
ClassA - exists
ClassA - update

That might or might not help you in this case.

-- 
How many Vietnam vets does it take to screw in a light bulb?
You don't know, man.  You don't KNOW.
Cause you weren't THERE.             http://bash.org/?255991

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