note 76205 added to language.oop5.interfaces

From: Date: Thu, 05 Jul 2007 04:01:08 +0000
Subject: note 76205 added to language.oop5.interfaces
Groups: php.notes 
Request: Send a blank email to php-notes+get-128239@lists.php.net to get a copy of this message
prometheus at php-sparcle: Your code fails because you're effectively trying to do this: <?php abstract class IFoo { abstract public function Foo(); } abstract class IBar extends IFoo { // Fails; abstract method IFoo::Foo() must be defined in child and must match parent's definition abstract public function Foo($bar); } ?> By definition, all methods in an interface are abstract. So the above code segment is equivalent to your interface definitions and results in the same error. Why? Let's have a look at the PHP manual. From the second paragraph on class abstraction: "When inheriting from an abstract class, all methods marked abstract in the parent's class declaration must be defined by the child;" http://www.php.net/manual/en/language.oop5.abstract.php So by definition, you may only overload non-abstract methods. For example: <?php abstract class IFoo { public function Foo() { // do something... } } abstract class IBar extends IFoo { public function Foo($bar) { // do something else... } } ?> This can't be directly replicated with interfaces since you can't implement methods inside of an interface. They can only be implemented in a class or an abstract class. If you must use interfaces, the following accomplishes the same thing, but with two separate method names: <?php interface IFoo { public function Foo(); } interface IBar extends IFoo { public function Bar($bar); } class FooBar implements IBar { public function Foo() { // do something... } public function Bar($bar) { // do something else... } } ?> If both methods need the same name, then you'll have to use non-abstract methods. In this case, interfaces aren't the right tool for the job. You'll want to use abstract classes (or just regular classes). ---- Server IP: 69.147.83.197 Probable Submitter: 66.243.216.101 ---- Manual Page -- http://www.php.net/manual/en/language.oop5.interfaces.php Edit -- https://master.php.net/note/edit/76205 Del: integrated -- https://master.php.net/note/delete/76205/integrated Del: useless -- https://master.php.net/note/delete/76205/useless Del: bad code -- https://master.php.net/note/delete/76205/bad+code Del: spam -- https://master.php.net/note/delete/76205/spam Del: non-english -- https://master.php.net/note/delete/76205/non-english Del: in docs -- https://master.php.net/note/delete/76205/in+docs Del: other reasons-- https://master.php.net/note/delete/76205 Reject -- https://master.php.net/note/reject/76205 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#128239) next »