In this article, we will discuss how to detect the type of arguments (such as class name or type) that a class method requires in PHP.
Table of Contents
Goal of this article
In PHP, we can specify class name or type for method arguments as follows.
The goal of this article is to get the class name or type of arguments that a class method requires.
class SampleClass{
public function sampleMethod(SmapleClass2 $arg1, array $arg2){
}
}
Way1: ReflectionMethod class
Generate an instance of the ReflectionMethod class by specifying the class name and method name you want to investigate as arguments.
You can obtain an array of ReflectionParameter objects using the getParameters method.
Then, the getType method can be used to get the type of the arguments.
$reflection = new ReflectionMethod('SampleClass', 'sampleMethod'); // ReflectionMethod objet
$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 array of class name and method 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(['SampleClass', 'sampleMethod'], 0))->getType(); // get the type of the first argument
echo (new ReflectionParameter(['SampleClass', 'sampleMethod'], 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(['SampleClass', 'sampleMethod'], 0))->getType();
echo $typeOfArg1; // SampleClass2 (string)
var_dump($typeOfArg1); // ReflectionNamedType (object)
class SampleClass{
public function sampleMethod(SmapleClass2 $arg1, array $arg2){
}
}
You can also use the getName method to formally retrieve it as a string.
$typeOfArg1 = (new ReflectionParameter(['SampleClass', 'sampleMethod'], 0))->getType();
echo $typeOfArg1; // SampleClass2 (string)
var_dump($typeOfArg1->getName()); // SampleClass2 (string)
class SampleClass{
public function sampleMethod(SmapleClass2 $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 getMethodArgType($class, $method, $position){
return (string)(new ReflectionParameter([$class, $method], $position))->getType();
}
// get the type of all arguments as an array
function getMethodArgTypeAll($class, $method){
$reflection = new ReflectionMethod($class, $method);
$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 class method requires in PHP.