In this article, we will discuss how to detect the type of arguments (such as class name or type) that a function requires in PHP.
Table of Contents
Goal of this article
In PHP, we can specify class name or type for function arguments as follows.
The goal of this article is to get the class name or type of arguments that a defined function requires.
function sampleFunc(SampleClass $arg1, array $arg2){
}
Way1: ReflectionFunction class
Generate an instance of the ReflectionFunction class by specifying the function name you want to investigate as arguments.
Use the getParameters method to obtain an array of ReflectionParameter objects.
Then, the getType method can be used to get the type of the arguments.
$reflection = new ReflectionFunction('sampleFunc'); // ReflectionFunction object
$parameters = $reflection->getParameters(); // ReflectionParameter objects array
echo $parameters[0]->getType(); // get the type of the first argument
echo $parameters[1]->getType(); // get the type of the second argument
Way2: ReflectionParameter class
You can also directly create a ReflectionParameter object.
By specifying the function name as the first argument and the position of the argument as the second argument, you can generate an instance.
The getType method can be used to get the type of the arguments.
echo (new ReflectionParameter('sampleFunc', 0))->getType(); // get the type of the first argument
echo (new ReflectionParameter('sampleFunc', 1))->getType(); // get the type of the second argument
Supplementary information about the getType method
The actual return value of the getType method is a ReflectionNamedType object. However, since the __toString method is implemented internally, it can also be treated as a string.
You can confirm that a ReflectionNamedType is returned by using var_dump.
$typeOfArg1 = (new ReflectionParameter('sampleFunc', 0))->getType();
echo $typeOfArg1; // SampleClass(string)
var_dump($typeOfArg1); // ReflectionNamedType(object)
function sampleFunc(SampleClass $arg1, array $arg2){
}
You can also use the getName method to formally retrieve it as a string.
$typeOfArg1 = (new ReflectionParameter('sampleFunc', 0))->getType();
echo $typeOfArg1; // SampleClass(string)
var_dump($typeOfArg1->getName()); // SampleClass(string)
function sampleFunc(SampleClass $arg1, array $arg2){
}
Wrap it in a function
It would be convenient to wrap it in a function, so here’s an example.
※Please note that if you are using PHP versions earlier than 7.4, you should replace the arrow function in the last line with a regular function.
// get the type of the specified argument
function getFuncArgType($funcName, $position){
return (string)(new ReflectionParameter($funcName, $position))->getType();
}
// get the type of all arguments as an array
function getFuncArgTypeAll($funcName){
$reflection = new ReflectionFunction($funcName);
$parameters = $reflection->getParameters();
return array_map(fn($param) => (string) $param->getType(), $parameters);
}
That is all, it was about how to detect the type of arguments that a function requires in PHP.