ReflectionMethodクラス、ReflectionPropertyクラス、ReflectionFunctionクラス
ReflectionMethodクラスでは、メソッドに対してリフレクションを適用して、解析されたメソッドについての個別情報を取得できます。ここでも、いくつかの例と類似するメソッドの名前を示します。
public void __construct(mixed class, string name)― ReflectionMethodのコンストラクタです。public mixed invoke(stdclass object [, mixed args [, ...]])― 解析されたメソッドを呼び出すときに使います。
// EXAMPLES //call a non-static method with no arguments $testClass = new TestClass(); $method = new ReflectionMethod('TestClass', 'testMethod_1'); echo $method->invoke($testClass); //call a non-static method with two arguments $testClass = new TestClass(); $method = new ReflectionMethod('TestClass', 'testMethod_2'); echo $method->invoke($testClass, 'testValue_1', 'testValue_2'); //call a static method with no arguments $method = new ReflectionMethod('TestClass', 'testMethod_3'); echo $method->invoke(NULL); //call a static method with two arguments $method = new ReflectionMethod('TestClass', 'testMethod_4'); echo $method->invoke(NULL,'testValue_1','testValue_2');
他にも、次のようなメソッドがあります。
public bool isPublic()― 解析されたメソッドがpublicの場合に、ブール値trueを返します。public bool isPrivate()― 解析されたメソッドがprivateの場合に、ブール値trueを返します。public bool isProtected()― 解析されたメソッドがprotectedの場合に、ブール値trueを返します。
// EXAMPLE $method = new ReflectionMethod('TestClass', 'testMethod_1'); echo $method->isPublic();
ReflectionMethodクラスでメソッドにリフレクションを適用するのと同じように、ReflectionPropertyクラスでは、プロパティにリフレクションを適用できます。このクラスのメソッドはReflectionMethodクラスのメソッドとほぼ同じなので、最小限の例のみを示すことにします。ReflectionPropertyオブジェクトの構築には次のメソッドを使用します。
public void __construct(mixed class, string name)
プロパティ値の取得には次のメソッドを使用します。
public mixed getValue(stdclass object):
getValueメソッドのパラメータにはクラスインスタンスを渡します。すると、そのインスタンスの対象プロパティの値が返ります。
$testClass= new TestClass(); $property = new ReflectionProperty('TestClass', 'testProperty_1'); echo $property->getValue($testClass);
値を設定するにはsetValueメソッドを使用し、クラスインスタンスと新しいプロパティ値の両方を渡します。使用例は次のとおりです。
$testClass= new TestClass(); $property = new ReflectionProperty('TestClass', 'testProperty_1'); echo 'Before : '.$property->getValue($testClass).'<br />'; $property->setValue($testClass,"new_testProperty_1"); echo 'After : '.$property->getValue($testClass);
ReflectionFunctionは、ReflectionMethodクラスやReflectionPropertyクラスと同様に、関数にリフレクションを適用するためのクラスです。関数を呼び出すにはinvokeメソッドを呼び出し、必要に応じてパラメータを渡します。構文は次のとおりです。
public mixed invoke([mixed args [, ...]])
簡単な例を次に示します。
$function = new ReflectionFunction('testFunction_1'); echo $function->invoke();
最後に紹介するのはReflectionParameterクラスです。関数やメソッドのパラメータにリフレクションを適用します。ReflectionParameterのインスタンスを作成するときには、関数名とパラメータの位置を渡します(位置は文字列で渡します。1つ目のパラメータはarg1、2つ目のパラメータはarg2、という形です)。
$parameter = new ReflectionParameter('testFunction_2','arg1'); echo $parameter->getName();
パラメータのデフォルト値を取得するには、getDefaultValueメソッドを使用します。また、isPassedByReferenceメソッドを使用すると、関数の特定のパラメータが参照渡しか値渡しかを調べることができます。パラメータが参照渡しの場合にはブール値のtrueが返ります。
リフレクションAPIには、ここで紹介した以外にも、ReflectionException、ReflectionObject、ReflectionExtension、ReflectionFunctionAbstractといったクラスがあり、それぞれのクラス名が示す項目についての情報を得ることができます。全体を網羅したドキュメントとチュートリアルについては、PHP5の公式マニュアルを参照してください。
