トレイトで抽象メソッドの定義

概要

抽象メソッドを定義して抽象化対応可能


サンプル

ソース

<?php
	/********************************
	 * トレイトの確認
	 ********************************/
 
	// トレイトの定義
	trait TestTrait01{
		// トレイト内部でメソッドの定義
		public function test01(){
			echo " traitTest1 ";
		}
 
		// 抽象メソッドを定義
		abstract public function test02();
	}
 
	// クラスの定義
	class TestClass01{
		use TestTrait01;
 
		// 抽象メソッドのオーバーライド
		public function test02(){
			echo "TestClass01->test02";
		}
 
	}
 
	/*******************************
	 * クラスを生成して実行
	 *******************************/
	$obj = new TestClass01();
	$obj->test01();
	echo "\n";
 
	$obj->test02();
	echo "\n";
 
 
 

結果

>php -f sample10.php
 traitTest1
TestClass01->test02
 
>
 

最終更新:2012年11月24日 23:37