ReflectionFunctionAbstract::getParameters

(PHP 5 >= 5.2.0, PHP 7, PHP 8)

ReflectionFunctionAbstract::getParameters β€” ΠŸΠΎΠ»ΡƒΡ‡Π°Π΅Ρ‚ ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€Ρ‹

ОписаниС

public function ReflectionFunctionAbstract::getParameters(): array

ΠŸΠΎΠ»ΡƒΡ‡Π΅Π½ΠΈΠ΅ ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ΠΎΠ² Π² Π²ΠΈΠ΄Π΅ массива ΠΎΠ±ΡŠΠ΅ΠΊΡ‚ΠΎΠ² ReflectionParameter Π² Ρ‚ΠΎΠΌ порядкС, Π² ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠΌ ΠΎΠ½ΠΈ ΠΎΠΏΡ€Π΅Π΄Π΅Π»Π΅Π½Ρ‹ Π² источникС.

Бписок ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ΠΎΠ²

Π‘ΠΈΠ³Π½Π°Ρ‚ΡƒΡ€Π° Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΈ Π½Π΅ содСрТит ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ΠΎΠ².

Π’ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅ΠΌΡ‹Π΅ значСния

ΠŸΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€Ρ‹, прСдставлСнныС ΠΎΠ±ΡŠΠ΅ΠΊΡ‚Π°ΠΌΠΈ ReflectionParameter.

Π‘ΠΌΠΎΡ‚Ρ€ΠΈΡ‚Π΅ Ρ‚Π°ΠΊΠΆΠ΅

  • ReflectionFunctionAbstract::getNumberOfParameters() - ΠŸΠΎΠ»ΡƒΡ‡Π°Π΅Ρ‚ количСство ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ΠΎΠ²
  • func_get_args() - Π’ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ массив, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ содСрТит список Π°Ρ€Π³ΡƒΠΌΠ΅Π½Ρ‚ΠΎΠ² Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΈ
οΌ‹Π”ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ

ΠŸΡ€ΠΈΠΌΠ΅Ρ‡Π°Π½ΠΈΡ ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»Π΅ΠΉ 2 notes

up
8
dabidi at slupca dot pl ΒΆ
10 years ago
This is part of my private framework that uses reflection. 
This function get arguments list from theme method and puts corresponding vars from $_REQUEST ($_GET, $_POST, and $_COOKIE)

<?php
public static function fire_theme_method($class, $method)
{
        $fire_args=array();
        
        $reflection = new ReflectionMethod($class, $method);

    foreach($reflection->getParameters() AS $arg)
    {
        if($_REQUEST[$arg->name])
        $fire_args[$arg->name]=$_REQUEST[$arg->name];
        else
        $fire_args[$arg->name]=null;
    }
        
    return call_user_func_array(array($class, $method), $fire_args);
}
?>
For example, if my theme method needs only id, and we get this url:
http://example.com/my_class/my_method/?id=12&some_unwanted_var=123
will be ignored some_unwanted_var

Of course behind this i have .htaccess, autoloader and controller
up
4
a dot lucassilvadeoliveira at gmail dot com ΒΆ
5 years ago
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.