The ReflectionFunction class
(PHP 5)
Introduction
The ReflectionFunction class reports information about a function.
Class synopsis
Properties
- name
-
Name of the function. Read-only, throws ReflectionException in attempt to write.
Predefined Constants
ReflectionFunction Modifiers
ReflectionFunction::IS_DEPRECATED
-
Indicates deprecated functions.
Table of Contents
- ReflectionFunction::__construct — Constructs a ReflectionFunction object
- ReflectionFunction::export — Exports function
- ReflectionFunction::getClosure — Returns a dynamically created closure for the function
- ReflectionFunction::invoke — Invokes function
- ReflectionFunction::invokeArgs — Invokes function args
- ReflectionFunction::isDisabled — Checks if function is disabled
- ReflectionFunction::__toString — To string
- PHP Руководство
- Функции по категориям
- Индекс функций
- Справочник функций
- Расширения, относящиеся к переменным и типам
- Введение
- Установка и настройка
- Предопределенные константы
- Примеры
- Расширение
- Класс Reflection
- Класс ReflectionClass
- Класс ReflectionZendExtension
- Класс ReflectionExtension
- Класс ReflectionFunction
- Класс ReflectionFunctionAbstract
- Класс ReflectionMethod
- Класс ReflectionObject
- Класс ReflectionParameter
- Класс ReflectionProperty
- ReflectionType
- ReflectionGenerator
- Интерфейс Reflector
- Класс ReflectionException
Коментарии
Here is a concise example of ReflectionFunction usage for Parameter Reflection / introspection (e.g. to automatically generate API descriptions)
<?php
$properties = $reflector->getProperties();
$refFunc = new ReflectionFunction('preg_replace');
foreach( $refFunc->getParameters() as $param ){
//invokes ■ReflectionParameter::__toString
print $param;
}
?>
prints:
Parameter #0 [ <required> $regex ]
Parameter #1 [ <required> $replace ]
Parameter #2 [ <required> $subject ]
Parameter #3 [ <optional> $limit ]
Parameter #4 [ <optional> &$count ]
We can use this functionality to automatically pass arguments to our function based on some data structure.
NOTE: I am using a php 8.0> feature called "Nameds parameter"
<?php
$valuesToProcess = [
'name' => 'Anderson Lucas Silva de Oliveira',
'age' => 21,
'hobbie' => 'Play games'
];
function processUserData($name, $age, $job = "", $hobbie = "")
{
$msg = "Hello $name. You have $age years old";
if (!empty($job)) {
$msg .= ". Your job is $job";
}
if (!empty($hobbie)) {
$msg .= ". Your hobbie is $hobbie";
}
echo $msg . ".";
}
$refFunction = new ReflectionFunction('processUserData');
$parameters = $refFunction->getParameters();
$validParameters = [];
foreach ($parameters as $parameter) {
if (!array_key_exists($parameter->getName(), $valuesToProcess) && !$parameter->isOptional()) {
throw new DomainException('Cannot resolve the parameter' . $parameter->getName());
}
if(!array_key_exists($parameter->getName(), $valuesToProcess)) {
continue;
}
$validParameters[$parameter->getName()] = $valuesToProcess[$parameter->getName()];
}
$refFunction->invoke(...$validParameters);
?>
Results in:
Hello Anderson Lucas Silva de Oliveira. You have 21 years old. Your hobbie is Play games.