The ReflectionMethod, ReflectionProperty, and ReflectionFunction Classes
You use the ReflectionMethod class to apply reflection to methods to obtain specific information about an introspected method. Again, I'll provide a few examples and a list of similar methods:
- public void __construct(mixed class, string name)—This is the ReflectionMethod constructor.
- public mixed invoke(stdclass object [, mixed args [, ...]])—Use this to call an introspected method.
// 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');
Some other available methods are:
- public bool isPublic()—Returns a Boolean true if the introspected method is public.
- public bool isPrivate()—Returns a Boolean true if the introspected method is private.
- public bool isProtected()—Returns a Boolean true if the introspected method is protected.
// EXAMPLE
$method = new ReflectionMethod('TestClass', 'testMethod_1');
echo $method->isPublic();
Just as you use the ReflectionMethod class to apply reflection to methods, you use the ReflectionProperty class to apply reflection to properties. The methods of this class are nearly identical to the ReflectionMethod class methods, so I'll show only a minimal set of examples here. To construct a ReflectionProperty object use:
public void __construct(mixed class, string name)
To get a property's value, use:
public mixed getValue(stdclass object):
The
getValue method takes a class instance parameter and returns the value of the corresponding introspected property for that instance:
$testClass= new TestClass();
$property = new ReflectionProperty('TestClass', 'testProperty_1');
echo $property->getValue($testClass);
To set a value, use the
setValue method and pass both a class instance and the new property value, for example:
$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);
The ReflectionFunction—much like the ReflectionMethod and ReflectionProperty classes, lets you apply reflection to functions. To invoke a function, call the
invoke method, passing parameters if required. The syntax is:
public mixed invoke([mixed args [, ...]])
Here's a simple example:
$function = new ReflectionFunction('testFunction_1');
echo $function->invoke();
Finally, the ReflectionParameter class applies reflection to function or method parameters. To create a ReflectionParameter instance, pass the name of the function and the parameter position (as strings where
arg1 is the first parameter,
arg2 is the second, and so forth):
$parameter = new ReflectionParameter('testFunction_2','arg1');
echo $parameter->getName();
To obtain the default value of a parameter, use the
getDefaultValue method. You can also discover whether a particular function parameter is passed by reference or by value using the
isPassedByReference method, which returns a Boolean
true if the parameter is passed by reference.
The Reflection API contains other classes not covered here, such as ReflectionException, ReflectionObject, ReflectionExtension, and ReflectionFunctionAbstract, which provide information about the types for which they're named. For complete documentation and a tutorial see the
official PHP5 manual.