(PHP 5, PHP 7, PHP 8)
ReflectionParameter::getClass — Obtiene un objeto ReflectionClass para el parámetro que se está reflejando o null
Esta función está OBSOLETA a partir de PHP 8.0.0. Depender de esta función está altamente desaconsejado.
Obtiene un objeto ReflectionClass para el parámetro que se está reflejando o null.
A partir de PHP 8.0.0 esta función está obsoleta y no se recomienda. En su lugar, debe utilizarse ReflectionParameter::getType() para obtener la ReflectionType de este parámetro y luego interrogar este objeto para determinar el tipo del parámetro.
Esta función está actualmente no documentada; solo la lista de sus argumentos está disponible.
Esta función no contiene ningún parámetro.
Un objeto ReflectionClass , o null si no se declara ningún tipo,
o el tipo declarado no es una clase o interfaz.
Ejemplo #1 Ejemplo de uso de la clase ReflectionParameter
<?php
function foo(Exception $a) { }
$functionReflection = new ReflectionFunction('foo');
$parameters = $functionReflection->getParameters();
$aParameter = $parameters[0];
echo $aParameter->getClass()->name;
?>| Versión | Descripción |
|---|---|
| 8.0.0 | Esta función ha sido deprecada en favor de ReflectionParameter::getType() . |
The method returns ReflectionClass object of parameter type class or NULL if none.
<?php
class A {
function b(B $c, array $d, $e) {
}
}
class B {
}
$refl = new ReflectionClass('A');
$par = $refl->getMethod('b')->getParameters();
var_dump($par[0]->getClass()->getName()); // outputs B
var_dump($par[1]->getClass()); // note that array type outputs NULL
var_dump($par[2]->getClass()); // outputs NULL
?>ReflectionParameter::getClass() will cause a fatal error (and trigger __autoload) if the class required by the parameter is not defined.
Sometimes it's useful to only know 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
function getClassName(ReflectionParameter $param) {
preg_match('/\[\s\<\w+?>\s([\w]+)/s', $param->__toString(), $matches);
return isset($matches[1]) ? $matches[1] : null;
}
?>For php version >=8
ReflectionParamter::getType() is the recommended way to replace the deprecated methods:
- getClass()
e.g: $name = $param->getType() && !$param->getType()->isBuiltin()
? new ReflectionClass($param->getType()->getName())
: null;
- isArray()
e.g: $isArray = $param->getType() && $param->getType()->getName() === 'array';
- isCallable()
e.g: $isCallable = $param->getType() && $param->getType()->getName() === 'callable';
This method is available in PHP 7.0 and later.You may use this one function instead depricated
/**
* Get parameter class
* @param \ReflectionParameter $parameter
* @return \ReflectionClass|null
*/
private function getClass(\ReflectionParameter $parameter):?\ReflectionClass
{
$type = $parameter->getType();
if (!$type || $type->isBuiltin())
return NULL;
// This line triggers autoloader!
if(!class_exists($type->getName()))
return NULL;
return new \ReflectionClass($type->getName());
}Example of how to use getClass() in conjunction with getConstructor() to build the dependencies of a class.
private function buildDependencies(ReflectionClass $reflection)
{
$constructor = $reflection->getConstructor();
if (!$constructor) {
return [];
}
$params = $constructor->getParameters();
return array_map(function ($param) {
$className = $param->getClass();
if (!$className) {
throw new Exception();
}
$className = $param->getClass()->getName();
return $this->make($className);
}, $params);
}