PHPのオブジェクトインターフェイス入門:基本構文から多重継承まで徹底解説
インターフェイスとは
インターフェイスは、オブジェクト指向プログラミングにおける重要な機能の一つです。クラスが実装すべきメソッドを「何をするか」という観点で指定できる一方、「どのように実装するか」までは定義しません。これにより、実装の詳細に縛られない柔軟で拡張性の高い設計が可能になります。
PHPではinterfaceキーワードを使ってインターフェイスを定義します。インターフェイスはクラスによく似ていますが、メソッドには処理本体がありません。また、インターフェイス内のメソッドは必ずpublicとして宣言する必要があります。インターフェイスを実装するクラスは、extendsではなくimplementsキーワードを使って宣言し、親インターフェイスで宣言されたすべてのメソッドを実装しなければなりません。
基本構文
<?php
interface testinterface{
public function testmethod();
}
class testclass implements testinterface{
public function testmethod(){
echo "implements interfce method";
}
}
?>インターフェイスで宣言されたすべてのメソッドは、実装するクラス側で必ず定義する必要があります。1つでも未定義のままにすると、PHPパーサーは致命的なエラーをスローします。
実装漏れがある場合の例
<?php
interface testinterface{
public function test1();
public function test2();
}
class testclass implements testinterface{
public function test1(){
echo "implements interface method";
}
}
$obj=new testclass()
?>出力
test2メソッドが実装されていないため、以下のようなエラーが発生します。
PHP Fatal error: Class testclass contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (testinterface::test2)
インターフェイスの継承
通常のクラスと同様に、インターフェイスもextendsキーワードを使って別のインターフェイスを継承できます。継承元のインターフェイスで宣言されたメソッドは、そのまま子インターフェイスにも引き継がれます。
次の例では、myinterfaceがtestinterfaceを継承しています。この場合、testclassは両方のインターフェイスで宣言されたすべてのメソッド(test1とtest2)を実装する必要があります。
例
<?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
実践的なインターフェイスの例
最後に、図形の面積を計算する実践的な例を見てみましょう。shapeインターフェイスがarea()メソッドを宣言し、circleクラスとrectangleクラスがそれぞれ独自の方法で面積計算を実装しています。このようにインターフェイスを使うことで、異なるクラスに共通の契約を持たせることができます。
例
<?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()関数とは?使い方とサンプルコードを解説
PHPのget_class()関数は、オブジェクトが属するクラス名を取得するための関数です。引数に渡された値がオブジェクトでない場合にはFALSEを返します。また、クラス内部でobject引数を省略して呼び出した場合には、そのクラス自身の名前が返されます。構文get_class(object)パラメータobject − 検査対象となるオブジェクトです。クラスの内部で呼び出す場合は、このパラメータを省略できます。戻り値get_class()関数は、指定されたオブジェクトのクラス名を文字列として返します。引数がオブジェクトでない場合はFALSEを返します。クラス内部
-
PHPのget_object_vars()関数の使い方を徹底解説
PHPのget_object_vars()関数は、指定されたオブジェクトが持つプロパティを取得するための関数です。オブジェクト内で定義されているプロパティを、プロパティ名をキーとした連想配列の形式で返します。 構文 get_object_vars(object) パラメータ object − プロパティを取得したいオブジェクトのインスタンスを指定します。 戻り値 get_object_vars()関数は、指定されたオブジェクトの定義済みプロパティを連想配列として返します。まだ値が代入されていないプロパティについては、NULLが返されます。 なお、この関数をクラスの外部から呼び出した場合、取