Re: PHP5 Inheritance/method override problem

From: Date: Wed, 06 Dec 2006 17:57:40 +0000
Subject: Re: PHP5 Inheritance/method override problem
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-245569@lists.php.net to get a copy of this message
# richard@hddbroker.com / 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

« previous php.general (#245569) next »