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
Почист и полокален преглед на PHP референцата, со задржана структура од PHP.net и подобра читливост за примери, секции и белешки.
ReflectionParameter::getClass
Референца за `reflectionparameter.getclass.php` со подобрена типографија и навигација.
ReflectionParameter::getClass
класата mysqli_driver
ReflectionParameter::getClass — Добијте ReflectionClass објект за параметарот што се рефлектира или null
Оваа функција е DEPRECATED од PHP 8.0.0. Силно се обесхрабрува потпирањето на оваа функција.
= NULL
public ReflectionParameter::getClass(): ?ReflectionClass
Добива еден ReflectionClass објект за параметарот што се рефлектира или null.
Од PHP 8.0.0 оваа функција е застарена и не се препорачува. Наместо тоа, користете се очекува, за да го добиете ReflectionType на параметарот, а потоа испитајте го тој објект за да го одредите типот на параметарот.
Оваа функција моментално не е документирана; достапна е само листата со аргументи.
Параметри
Оваа функција нема параметри.
Вратени вредности
А ReflectionClass објект, или null ако не е деклариран тип, или декларираниот тип не е класа или интерфејс.
Примери
Пример #1 Користење на ReflectionParameter class
<?php
function foo(Exception $a) { }
$functionReflection = new ReflectionFunction('foo');
$parameters = $functionReflection->getParameters();
$aParameter = $parameters[0];
echo $aParameter->getClass()->name;
?>Дневник на промени
| Верзија | = NULL |
|---|---|
| 8.0.0 | Оваа функција е отпишана во корист на се очекува, instead. |
Белешки од корисници 5 белешки
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);
}