PHPオブジェクトインターフェイス
はじめに
インターフェイスはオブジェクト指向プログラミングの重要な機能であり、実装方法を定義しなくても、クラスで実装するメソッドを指定できます。
インターフェースの場合、PHPはインターフェースをサポートします キーワード。インターフェイスはクラスに似ていますが、定義本体のないメソッドがあります。インターフェイスのメソッドはパブリックである必要があります。これらのメソッドを実装する継承クラスは、実装で定義する必要があります extendskeywordの代わりにkeywordであり、親インターフェースのすべてのメソッドの実装を提供する必要があります。
構文
<?php interface testinterface{ public function testmethod(); } class testclass implements testinterface{ public function testmethod(){ echo "implements interfce method"; } } ?>
インターフェイスのすべてのメソッドは実装クラスで定義する必要があります。定義しない場合、PHPパーサーは例外をスローします
例
<?php interface testinterface{ public function test1(); public function test2(); } class testclass implements testinterface{ public function test1(){ echo "implements interface method"; } } $obj=new testclass() ?>
出力
エラーは以下のとおりです-
PHP Fatal error: Class testclass contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (testinterface::test2)
拡張可能なインターフェース
通常のクラスと同様に、 extends を使用して、インターフェースを継承することもできます。 キーワード。
次の例では、親クラスに2つの抽象メソッドがあり、そのうちの1つだけが子クラスで再定義されます。これにより、次のようなエラーが発生します-
例
<?php interface testinterface{ public function test1(); } interface myinterface extends testinterface{ public function test2(); } class testclass implements myinterface{ public function test1(){ echo "implements test1 method"; } public function test2(){ echo "implements test2 method"; } } ?>
インターフェースを使用した多重継承
PHPでは、extends句で複数のクラスを使用することはできません。ただし、子クラスに1つ以上のインターフェイスを実装させることで、多重継承を実現できます
次の例では、myclassはtestclassを拡張し、testinterfaceを実装して多重継承を実現します
例
<?php interface testinterface{ public function test1(); } class testclass{ public function test2(){ echo "this is test2 function in parent class\n"; } } class myclass extends testclass implements testinterface{ public function test1(){ echo "implements test1 method\n"; } } $obj=new myclass(); $obj->test1(); $obj->test2(); ?>
出力
これにより、次の出力が生成されます-
implements test1 method this is test2 function in parent class
インターフェースの例
例
<?php interface shape{ public function area(); } class circle implements shape{ private $rad; public function __construct(){ $this->rad=5; } public function area(){ echo "area of circle=" . M_PI*pow($this->rad,2) ."\n"; } } class rectangle implements shape{ private $width; private $height; public function __construct(){ $this->width=20; $this->height=10; } public function area(){ echo "area of rectangle=" . $this->width*$this->height ."\n"; } } $c=new circle(); $c->area(); $r=new rectangle(); $r->area(); ?>
出力
上記のスクリプトは次の結果を生成します
area of circle=78.539816339745 area of rectangle=200
-
PHPのget_class()関数
get_class()関数は、オブジェクトのクラスの名前を取得します。オブジェクトがオブジェクトでない場合はFALSEを返します。クラス内でオブジェクトが除外されると、そのクラスの名前が返されます。 構文 get_class(object) パラメータ オブジェクト −テストされたオブジェクト。キャス内でこのパラメーターを回避できます。 戻る get_class()関数は、オブジェクトのクラスの名前を返します。オブジェクトがオブジェクトでない場合はFALSEを返します。クラス内でオブジェクトが除外されると、そのクラスの名前が返されます。 例 以下は例です- <?php cl
-
PHPのget_object_vars()関数
get_object_var()関数は、指定されたオブジェクトのプロパティを取得します。指定されたオブジェクトに対して定義されたオブジェクトプロパティの連想配列を返します。 構文 get_object_vars(object) パラメータ オブジェクト −オブジェクトインスタンス。 戻る get_object_var()関数は、指定されたオブジェクトに対して定義されたオブジェクトプロパティの連想配列を返します。プロパティに値が割り当てられていない場合は、NULL値で返されます。 例 以下は例です- <?php class Point2D { &nb