複数のインターフェイスを実装する際のメソッド名衝突

 <?php
 interface Interface1 {
     public function foo();
     public function bar();
 }
 interface Interface2 {
     public function bar();
 }
 class Class1 implements Interface1,Interface2 {
     public function foo() {}
     public function bar() {}
 }

↓これを実行するとbar()が衝突する。つまり、PHPのインターフェイスは多重継承っぽく使う場合のメソッド名衝突回避にはならないという事?

 PHP Fatal error:  Can't inherit abstract function Interface2::bar() (previously declared abstract in Interface1) 
 in /home/taro/tmp/Class1.php on line 9

↓ちなみにJavaやC#だとOK。

 interface Interface1 {
     void foo();
     void bar();
 }
 interface Interface2 {
     void bar();
 }
 public class Class1 implements Interface1, Interface2 {
   public static void main( String args[] ) { }
   public void foo() { }
   public void bar() { }
 }
 interface Interface1 {
     void foo();
     void bar();
 }
 interface Interface2 {
     void bar();
 }
 class Class1 : Interface1, Interface2
 {
   [STAThread]
   static void Main(string[] args) { }
   public void foo() {}
   public void bar() {}
 }

参考

http://www.php.net/manual/ja/language.oop5.interfaces.php


トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS

Last-modified: 2012-01-15 (日) 02:27:02