2009-12-09 11 views
52

PHP 5ではプライベート/保護されたメソッドを持つインタフェースを持つことは可能ですか?private/protectedメソッドを持つインターフェイスを使用することはできますか?

は、今私が持っている:

エラーがスローされます
interface iService 
{ 
    private method1(); 
} 

Parse error: syntax error, unexpected T_STRING, expecting T_VARIABLE

私はちょうどそれがインターフェースはパブリックメソッドのみを含めることができる場合であることの確認を持っていると思います。

+2

私は答えが失望していることがわかります。私は、保護された/私的な方法をサポートするインターフェースも望みます。たとえば、抽象クラスで実装されたパブリックメソッドは、サブクラスによって実装される保護されたメソッドに依存するクラスを持っています。私は、サブクラスが抽象パブリックメソッドで必要とされる保護されたメソッドを実装するようにインターフェイスを使用する必要があります。 – Stoutie

+3

その目的で抽象基本クラスを使用します。 2つのアプローチを組み合わせることができます。インタフェース内のパブリックメソッド、抽象メソッドを定義する抽象的な基本クラス内のメソッドの実装 –

+2

プライベートメソッドまたはプロテクトメソッドを宣言できれば、 'private; private method1();ではなく' private function method1(); 'となります。 – turibe

答えて

88

PHP manual page about interfacesは、明示的に述べている:

All methods declared in an interface must be public, this is the nature of an interface.

を私は、これはあなたが;-)

8

一般に、インターフェイスの唯一の機能は継承されるため、インターフェイスにはパブリックメンバーしか存在できません。 PHPfreaks.comチュートリアルから

PHP5 features interfaces. Not to be confused with interfaces in the more general sense, the interface keyword creates an entity that can be used to enforce a common interface upon classes without having to extend them like with abstract classes. Instead an interface is implemented.

Interfaces are different from abstract classes. For one, they’re not actually classes. They don’t define properties, and they don’t define any behaviour. The methods declared in an interface must be declared in classes that implement it.

Because an interface in the more general sense is a definition of how an object interacts with other code, all methods must be declared public (see section on visibility in this chapter). Using abstract classes, an abstract method can have any visibility, but the extending classes must have their implementations use the same (or weaker) visibility. Implementing an interface adds the methods as abstract methods to the subject class, failure to implement it will result in an error like the following:

Fatal error: Class SomeConcreteClass contains n abstract method(s) and must therefore be declared abstract or implement the remaining methodsYes, abstract classes can implement interfaces.

+2

あまりにも悪いです。私はインターフェイスで必要とされるパブリックメソッドを持っていたいと思っています。抽象クラスで実装されているインターフェイスは、インターフェイスによって強制される保護されたメソッドに依存しています。このようにして、抽象クラスはパブリックインターフェイスを提供できますが、基本ロジックを実装するのはサブクラスに依存します。理にかなっている? – Stoutie

+3

サブクラスによって実装されるメソッドが抽象抽象であるように思えます。それから、どんなサブクラスもそれを実装しなければなりません。しかし、それはインターフェイスとは関係ありません。 – Sven

20

インタフェースは、そのインタフェースを実装するクラスのパブリックメソッドを記述するために使用されています。あなたは決してインターフェイスのプライベートメソッドを持つことはできません。インタフェース内のメソッドは使用中であるとみなされるため、変更しないでください。

InterfacesはPHPリンクですが、これはOOプログラミングの標準です。

+0

はjavaのような他の言語では、インタフェースでアクセス修飾子を使用できます。 – BRjava

6

インターフェイスは、型宣言されている取得しているエラーを説明すると思います。タイプは値のセットと、外部からそれらの上に運ばれる操作のセットです。プライベートな方法はこの写真に収まりません。

interface T { 
    public /*int*/ function f(array $a); 
} 
interface U { 
    public /*T*/ function g(T $t); 
} 

class C implements U { 
    public function g(T $t) { 
     ... 
     $x = $t->f(); 
     ... 
    } 
} 

インターフェイスは、オブジェクトのインターフェイスを正しく示しているので便利です。オブジェクトが環境とどのように通信するか。

T::fを非公開と宣言できたとします。それが他のオブジェクトにどのように役立つでしょうか?それは外部から呼び出すことはできません、それはそのインターフェイスの一部ではありません。

3

多くの場合、インターフェイス定義は、他のモジュールがクラスの動作とAPIを保証するのに役立ちます。その場合、他のモジュールがプライベートなものにアクセスしたり理解したりすることはできません。そのため、プライベートメソッドを決してインターフェイスに置くことはできません。

関連する問題