ReflectionParameter::getClass

(PHP 5, PHP 7, PHP 8)

ReflectionParameter::getClassリフレクションされた ReflectionClass を取得する。

警告

この関数は PHP 8.0.0 で 非推奨になります。この関数に頼らないことを強く推奨します。

説明

public ReflectionParameter::getClass(): ?ReflectionClass

パラメータとしてリフレクションされたクラスを、 ReflectionClass オブジェクトとして取得するか、null を返します。

PHP 8.0.0 以降は、この関数を使うことは推奨されなくなりました。 代わりに、引数の型を調べるために ReflectionType を取得して下さい。 それを取得するには、ReflectionParameter::getType() を使います。

警告

この関数は、 現在のところ詳細な情報はありません。引数のリストのみが 記述されています。

パラメータ

この関数にはパラメータはありません。

戻り値

ReflectionClass オブジェクトを返します。 型が宣言されていないか、 宣言されていてもクラスやインターフェイスでない場合は、null を返します。

例1 ReflectionParameter クラスの利用法

<?php
function foo(Exception $a) { }

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

echo
$aParameter->getClass()->name;
?>

参考

add a note

User Contributed Notes 5 notes

up
12
infernaz at gmail dot com
14 years ago
The method returns ReflectionClass object of parameter type class or NULL if none.<?phpclass 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 Bvar_dump($par[1]->getClass());  // note that array type outputs NULLvar_dump($par[2]->getClass());  // outputs NULL?>
up
10
tom at r dot je
13 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:<?phpfunction getClassName(ReflectionParameter $param) {    preg_match('/\[\s\<\w+?>\s([\w]+)/s', $param->__toString(), $matches);    return isset($matches[1]) ? $matches[1] : null;}?>
up
4
Dylan
4 years ago
For php version >=8ReflectionParamter::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
4 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
5 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