ReflectionParameter::getClass

(PHP 5, PHP 7, PHP 8)

ReflectionParameter::getClass — Récupère un objet ReflectionClass pour le paramètre étant reflété ou null

Avertissement

Cette fonction est OBSOLÈTE à partir de PHP 8.0.0. Dépendre de cette fonction est fortement déconseillé.

Description

#[\Deprecated(
since: '8.0',
message: "use ReflectionParameter::getType() instead",
)]

public function ReflectionParameter::getClass(): ? ReflectionClass

Récupère un objet ReflectionClass pour le paramètre étant reflété ou null.

À partir de PHP 8.0.0 cette fonction est obsolète et n'est pas recommandée. À la place, utiliser ReflectionParameter::getType() pour récupérer la ReflectionType de ce paramètre puis interroger cet objet pour déterminer le type du paramètre.

Avertissement

Cette fonction est actuellement non documentée ; seule la liste des arguments est disponible.

Liste de paramètres

Cette fonction ne contient aucun paramètre.

Valeurs de retour

Un objet ReflectionClass , ou null si aucun type n'est déclaré, ou le type déclaré n'est pas une classe ou interface.

Exemples

Exemple #1 Exemple d'utilisation de la classe ReflectionParameter

<?php
function foo(Exception $a) { }
$functionReflection = new ReflectionFunction('foo');
$parameters = $functionReflection->getParameters();
$aParameter = $parameters[0];
echo $aParameter->getClass()->name;
?>

Historique

Version Description
8.0.0 Cette fonction a été dépréciée au profit de ReflectionParameter::getType() .

Voir aussi

Found A Problem?

Learn How To Improve This Page • Submit a Pull Request • Report a Bug
+add a note

User Contributed Notes 5 notes

up
11
infernaz at gmail dot com
15 years ago
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
?>
up
10
tom at r dot je
14 years ago
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;
}
?>
up
7
Dylan
5 years ago
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.
up
1
tarik at bitstore dot ru
5 years ago
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());
 }
up
-4
richard dot t dot rohrig at gmail dot com
6 years ago
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);
 }
+add a note

AltStyle によって変換されたページ (->オリジナル) /