(PHP 5, PHP 7, PHP 8)

ReflectionParameter::getClassGet 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 discouraged.


public ReflectionParameter::getClass(): ?ReflectionClass

Gets 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::getType() to get the ReflectionType of the parameter, then interrogate that object to determine the parameter type.


Questa funzione, al momento non è documentata; è disponibile soltanto la lista degli argomenti.

Elenco dei parametri

Questa funzione non contiene parametri.

Valori restituiti

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

function foo(Exception $a) { }

$functionReflection = new ReflectionFunction('foo');
$parameters = $functionReflection->getParameters();
$aParameter = $parameters[0];


Vedere anche:

add a note add a note

User Contributed Notes 5 notes

infernaz at gmail dot com
12 years ago
The method returns ReflectionClass object of parameter type class or NULL if none.


class A {
b(B $c, array $d, $e) {
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

tom at r dot je
11 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:

function getClassName(ReflectionParameter $param) {
preg_match('/\[\s\<\w+?>\s([\w]+)/s', $param->__toString(), $matches);
    return isset(
$matches[1]) ? $matches[1] : null;
tarik at bitstore dot ru
2 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!
            return NULL;

        return  new \ReflectionClass($type->getName());
2 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.
richard dot t dot rohrig at gmail dot com
3 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);
To Top