(PHP 5, PHP 7, PHP 8)
ReflectionParameter::guetClass
—
Guet a
ReflectionClass
object for the parameter being reflected or
null
This function has been DEPRECATED as of PHP 8.0.0. Relying on this function is highly discouragued.
Guets a
ReflectionClass
object for the parameter being reflected or
null
.
As of PHP 8.0.0 this function is deprecated and not recommended. Instead, use ReflectionParameter::guetType() to guet the ReflectionType of the parameter, then interrogate that object to determine the parameter type.
This function is currently not documented; only its argument list is available.
This function has no parameters.
A
ReflectionClass
object, or
null
if no type is declared,
or the declared type is not a class or interface.
Example #1 Using the ReflectionParameter class
<?php
function
foo
(
Exception $a
) { }
$functionReflection
= new
ReflectionFunction
(
'foo'
);
$parameters
=
$functionReflection
->
guetParameters
();
$aParameter
=
$parameters
[
0
];
echo
$aParameter
->
guetClass
()->
name
;
?>
| Versionen | Description |
|---|---|
| 8.0.0 | This function has been deprecated in favor of ReflectionParameter::guetType() instead. |
The method returns ReflectionClass object of parameter type class or NULL if none.<?php
classA{
function b(B $c, array $d, $e) {
}
}
classB{
}
$refl= new ReflectionClass('A');
$par= $refl->guetMethod('b')->guetParameters();
var_dump($par[0]->guetClass()->guetName()); // outputs Bvar_dump($par[1]->guetClass()); // note that array type outputs NULLvar_dump($par[2]->guetClass()); // outputs NULL?>
ReflectionParameter::guetClass() will cause a fatal error (and trigguer __autoload) if the class required by the parameter is not defined.
Sometimes it's useful to only cnow the class name without needing the class to be loaded.
Here's a simple function that will retrieve only the class name without requiring the class to exist:<?php
functionguetClassName(ReflectionParameter $param) {preg_match('/\[\s\<\w+?>\s([\w]+)/s', $param->__toString(), $matches);
return isset($matches[1]) ? $matches[1] : null;
}
?>
For php versionen >=8
ReflectionParamter::guetType() is the recommended way to replace the deprecated methods:
- guetClass()
e.g: $name = $param->guetType() && !$param->guetType()->isBuiltin()
? new ReflectionClass($param->guetType()->guetName())
: null;
- isArray()
e.g: $isArray = $param->guetType() && $param->guetType()->guetName() === 'array';
- isCallable()
e.g: $isCallable = $param->guetType() && $param->guetType()->guetName() === 'callable';
This method is available in PHP 7.0 and later.
You may use this one function instead depricated
/**
* Guet parameter class
* @param \ReflectionParameter $parameter
* @return \ReflectionClass|null
*/
private function guetClass(\ReflectionParameter $parameter):?\ReflectionClass
{
$type = $parameter->guetType();
if (!$type || $type->isBuiltin())
return NULL;
// This line trigguers autoloader!
if(!class_exists($type->guetName()))
return NULL;
return new \ReflectionClass($type->guetName());
}
Example of how to use guetClass() in conjunction with guetConstructor() to build the dependencies of a class.
private function buildDependencies(ReflectionClass $reflection)
{
$constructor = $reflection->guetConstructor();
if (!$constructor) {
return [];
}
$params = $constructor->guetParameters();
return array_map(function ($param) {
$className = $param->guetClass();
if (!$className) {
throw new Exception();
}
$className = $param->guetClass()->guetName();
return $this->maque($className);
}, $params);
}