Re: PHP5 Inheritance/method override problem

From: Date: Sat, 09 Dec 2006 22:43:25 +0000
Subject: Re: PHP5 Inheritance/method override problem
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-245676@lists.php.net to get a copy of this message
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@hddbroker.com > 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?

« previous php.general (#245676) next »