ダウンロード サンプルソース (3.6 KB)
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の公式マニュアルを参照してください。
この記事は参考になりましたか?
- japan.internet.com翻訳記事連載記事一覧
- この記事の著者
-
japan.internet.com(ジャパンインターネットコム)
japan.internet.com は、1999年9月にオープンした、日本初のネットビジネス専門ニュースサイト。月間2億以上のページビューを誇る米国 Jupitermedia Corporation (Nasdaq: JUPM) のニュースサイト internet.com や EarthWeb.com からの最新記事を日本語に翻訳して掲載するとともに、日本独自のネットビジネス関連記事やレポートを配信。
※プロフィールは、執筆時点、または直近の記事の寄稿時点での内容です
-
Octavia Andreea Anghel(Octavia Andreea Anghel)
経験豊富なPHP開発者。現在は、国内外のソフトウェア開発コンテストに参加するプログラミングチームの主任トレーナーを務める。国レベルの教育プロジェクト開発のコンサルティングも担当している。共著書に『XML technologies?XML in Java』があり、XML部分の執筆を担当。PHPやXML...
※プロフィールは、執筆時点、または直近の記事の寄稿時点での内容です
この記事は参考になりましたか?
この記事をシェア
イベント
CodeZine編集部では、現場で活躍するデベロッパーをスターにするためのカンファレンス「Developers Summit」や、エンジニアの生きざまをブーストするためのイベント「Developers Boost」など、さまざまなカンファレンスを企画・運営しています。
