PHP tool to reflect callables an types.
Use composer command:
$ composer require raphhh/trex-reflection
You can use CallableReflection to inspect and call a callable, like a callback or a Closure for example.
You can know which kind of callable is given.
$reflect = new CallableReflection(function(){});
$reflect->isClosure(); //true
$reflect = CallableReflection('in_array')
$reflect->isFunction(); //true
$reflect = new CallableReflection('\DateTime::createFromFormat');
$reflect->isMethod(); //true
$reflect->isStaticMethod(); //true
$reflect = new CallableReflection(array('\DateTime', 'createFromFormat'));
$reflect->isMethod(); //true
$reflect->isStaticMethod(); //true
$reflect = new CallableReflection(array(new \DateTime(), 'modify'));
$reflect->isMethod(); //true
$reflect->isInstanceMethod(); //true
class Bar{
function __invoke(){}
}
$reflect = new CallableReflection(new Bar());
$reflect->isInvokedObject(); //true
You can retrieve the callable, like the object or the method name for example.
$reflect = new CallableReflection(function(){});
$reflect->getClosure(); //closure
$reflect = new CallableReflection('in_array')
$reflect->getFunctionName(); //'in_array'
$reflect = new CallableReflection('\DateTime::createFromFormat');
$reflect->getClassName(); //'DateTime'
$reflect->getMethodName(); //'createFromFormat'
$reflect = new CallableReflection(array('\DateTime', 'createFromFormat'));
$reflect->getClassName(); //'DateTime'
$reflect->getMethodName(); //'createFromFormat'
$reflect = new CallableReflection(array(new \DateTime(), 'modify'));
$reflect->getClassName(); //'DateTime'
$reflect->getObject(); //DateTime instance
$reflect->getMethodName(); //'modify'
class Bar{
function __invoke(){}
}
$reflect = new CallableReflection(new Bar());
$reflect->getClassName(); //'Bar'
$reflect->getObject(); //Bar instance
You can invoke every kind of callable in a same way.
This method calls the method and give to it all its args.
$reflect = new CallableReflection('in_array')
$reflect->invoke(1, [0, 1]); //true
This method allows to map each value of an array with every params of a function. Useful to use dynamic args with func_get_args().
$reflect = new CallableReflection('in_array')
$reflect->invoke([1, [0, 1]]); //true
This method allows to map the keys of an array with the name of the params of a function. So, the order of the args has no importance.
$closure = function($arg1, $arg2){
return [$arg1, $arg2];
}
$reflect = new CallableReflection($closure)
$reflect->invokeA(['arg2' => 'arg2', 'arg1' => 'arg1'])); //['arg1', 'arg2']
Retrieve the ReflectionFunctionAbstract of the callable.
$reflect = new CallableReflection('in_array');
$reflect->getReflector(); //ReflectionFunction
$reflect = new CallableReflection(array('\DateTime', 'createFromFormat'));
$reflect->getReflector(); //ReflectionMethod
Note that for a class, we get a ReflectionMethod
for the __invoke
method of the current object, and not a ReflectionClass
.
class Bar{
function __invoke(){}
}
$reflect = new CallableReflection(new Bar());
$reflect->getReflector(); //ReflectionMethod
Reflection on the type of a variable or function.
A type is a string defined by a specific value related to one of the PHP types.
$typeReflection = new TypeReflection('int');
$typeReflection->getType(); //"int"
$typeReflection->getStandardizedType(); //"integer"
If you work with a variable, you can use TypeReflection::createFromVariable($variable)
$foo = new stdClass();
$typeReflection = TypeReflection::createFromVariable($foo);
$typeReflection->getType(); //"stdClass"
$typeReflection->getStandardizedType(); //"object"
Note the TypeReflection
is case insensitive.
TypeReflection
standardizes types with following values:
void
mixed
null
boolean
string
integer
float
number
scalar
array
object
resource
callable
unknown type
$typeReflection = new TypeReflection('string');
$typeReflection->isValid(); //true
$typeReflection = new TypeReflection('foo');
$typeReflection->isValid(); //false
$typeReflection = new TypeReflection('bool');
$typeReflection->isBoolean(); //true
$typeReflection = new TypeReflection('boolean');
$typeReflection->isBoolean(); //true
$typeReflection = new TypeReflection('string');
$typeReflection->isString(); //true
$typeReflection = new TypeReflection('int');
$typeReflection->isInteger(); //true
$typeReflection = new TypeReflection('integer');
$typeReflection->isInteger(); //true
$typeReflection = new TypeReflection('long');
$typeReflection->isInteger(); //true
$typeReflection = new TypeReflection('float');
$typeReflection->isFloat(); //true
$typeReflection = new TypeReflection('double');
$typeReflection->isFloat(); //true
$typeReflection = new TypeReflection('real');
$typeReflection->isFloat(); //true
Any integer or float value.
Any boolean, string or number value.
$typeReflection = new TypeReflection('array');
$typeReflection->isArray(); //true
$typeReflection = new TypeReflection('int[]');
$typeReflection->isArray(); //true
$typeReflection = new TypeReflection('object');
$typeReflection->isObject(); //true
$typeReflection = new TypeReflection('Datetime');
$typeReflection->isObject(); //true
$typeReflection = new TypeReflection('resource');
$typeReflection->isResource(); //true
$typeReflection = new TypeReflection('callable');
$typeReflection->isCallable(); //true
$typeReflection = new TypeReflection('void');
$typeReflection->isVoid(); //true
$typeReflection = new TypeReflection('null');
$typeReflection->isNull(); //true
$typeReflection = new TypeReflection('mixed');
$typeReflection->isMixed(); //true
$typeReflection = new TypeReflection('bool');
$typeReflection->getStandardizedType(); //boolean
$typeReflection = new TypeReflection('int');
$typeReflection->getStandardizedType(); //integer
$typeReflection = new TypeReflection('real');
$typeReflection->getStandardizedType(); //float
$typeReflection = new TypeReflection('int[]');
$typeReflection->getStandardizedType(); //array
$typeReflection = new TypeReflection('Datetime');
$typeReflection->getStandardizedType(); //object