インターフェース
基本
1つのクラスに複数のインターフェースを定義できる
インターフェース自体の継承も可能
それぞれのクラスで呼び出し元のメソッドを共通化したい場合などに使用
基本
<?php
// インターフェース定義
interface i_interface01
{
// メソッド定義
public function test_func01();
public function test_func02();
// public function test_func03(); // コメントを解除したらメソッド未定義でエラーとなる
}
// クラス定義
class TestClass implements i_interface01
{
public function test_func01(){
echo "i_interface01->test_func01\n";
}
public function test_func02(){
echo "i_interface01->test_func02\n";
}
}
$obj = new TestClass();
$obj->test_func01();
$obj->test_func02();
?>
複数のインターフェースを使用
<?php
// インターフェース定義
interface i_interface01
{
// メソッド定義
public function test_func01();
public function test_func02();
}
interface i_interface02
{
// メソッド定義
public function test_func03();
public function test_func04();
}
// クラス定義
class TestClass implements i_interface01, i_interface02
{
public function test_func01(){
echo "i_interface01->test_func01\n";
}
public function test_func02(){
echo "i_interface01->test_func02\n";
}
public function test_func03(){
echo "i_interface02->test_func03\n";
}
public function test_func04(){
echo "i_interface02->test_func04\n";
}
}
$obj = new TestClass();
$obj->test_func01();
$obj->test_func02();
$obj->test_func03();
$obj->test_func04();
?>
インターフェースの継承
<?php
// インターフェース定義
interface i_interface01
{
// メソッド定義
public function test_func01();
public function test_func02();
}
interface i_interface02
{
// メソッド定義
public function test_func03();
}
interface i_interface03 extends i_interface01, i_interface02
{
// メソッド定義
public function test_func04();
}
// クラス定義
class TestClass implements i_interface03
{
public function test_func01(){
echo "i_interface01->test_func01\n";
}
public function test_func02(){
echo "i_interface01->test_func02\n";
}
public function test_func03(){
echo "i_interface02->test_func03\n";
}
public function test_func04(){
echo "i_interface03->test_func04\n";
}
}
$obj = new TestClass();
$obj->test_func01();
$obj->test_func02();
$obj->test_func03();
$obj->test_func04();
?>
最終更新:2012年08月12日 13:21