メソッド調査

概要

method_exists

get_class_methods


サンプル(method_exists)クラスの有無をチェック

<?php
// 親クラスなし
class TestClass01{
	// コンストラクタ
	function __construct(){
		echo "コンストラクタ\n";
	}
 
	// サンプルメソッド
	public function testMethod01(){
		echo "testMethod01\n";
	}
 
	// サンプルメソッド
	private function testMethod02(){
		echo "testMethod02\n";
	}
 
	// サンプルメソッド
	protected function testMethod03(){
		echo "testMethod03\n";
	}
 
	// デストラクタ
	function __destruct(){
		echo "デストラクタ\n";
	}
}
?>
<?php
	// TestClass01オブジェクト生成
	$obj1 = new TestClass01();
 
	// メソッドチェック
	$res1 = method_exists($obj1, "__construct");
	var_dump($res1);
	$res2 = method_exists($obj1, "testMethod01");
	var_dump($res2);
	$res3 = method_exists($obj1, "testMethod02");
	var_dump($res3);
	$res4 = method_exists($obj1, "testMethod03");
	var_dump($res4);
	$res5 = method_exists($obj1, "__destruct");
	var_dump($res5);
	$res6 = method_exists($obj1, "testMethod04");
	var_dump($res6);
?>
 
 
 

サンプル(get_class_methods)クラスのメソッド名を取得 ※public定義のみ対象

<?php
// 親クラスなし
class TestClass01{
	// コンストラクタ
	function __construct(){
		echo "コンストラクタ\n";
	}
 
	// サンプルメソッド
	public function testMethod01(){
		echo "testMethod01\n";
	}
 
	// サンプルメソッド
	private function testMethod02(){
		echo "testMethod02\n";
	}
 
	// サンプルメソッド
	protected function testMethod03(){
		echo "testMethod03\n";
	}
 
	// デストラクタ
	function __destruct(){
		echo "デストラクタ\n";
	}
}
?>
<?php
	// TestClass01のメソッド一覧取得
	$methods = get_class_methods("TestClass01");
 
	// メソッドを表示
	// public定義のものが対象となる
	var_dump($methods);
?>
 
 
 


最終更新:2012年08月17日 21:54