Movatterモバイル変換


[0]ホーム

URL:


PHP 8.5.0 Alpha 2 available for testing
    ReflectionFunction::__construct »
    « ReflectionExtension::__toString

    The ReflectionFunction class

    (PHP 5, PHP 7, PHP 8)

    Introduction

    TheReflectionFunction class reports information about a function.

    Class synopsis

    classReflectionFunctionextendsReflectionFunctionAbstract {
    /* Constants */
    publicconstintIS_DEPRECATED;
    /* Inherited properties */
    /* Methods */
    public__construct(Closure|string$function)
    publicstaticexport(string$name,string$return = ?):string
    publicinvoke(mixed...$args):mixed
    publicinvokeArgs(array$args):mixed
    /* Inherited methods */
    }

    Predefined Constants

    ReflectionFunction Modifiers

    ReflectionFunction::IS_DEPRECATEDint

    Indicates deprecated functions.

    Changelog

    VersionDescription
    8.4.0 The class constants are now typed.
    8.0.0ReflectionFunction::export() was removed.

    Table of Contents

    Found A Problem?

    Learn How To Improve This PageSubmit a Pull RequestReport a Bug
    add a note

    User Contributed Notes2 notes

    a dot lucassilvadeoliveira at gmail dot com
    4 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= newReflectionFunction('processUserData');
    $parameters=$refFunction->getParameters();

    $validParameters= [];
    foreach (
    $parametersas$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.
    Lorenz R.S.
    13 years ago
    Here is a concise example of ReflectionFunction usage for Parameter Reflection / introspection (e.g. to automatically generate API descriptions)

    <?php
    $properties
    =$reflector->getProperties();
    $refFunc= newReflectionFunction('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 ]
    add a note
    To Top
    and to navigate •Enter to select •Esc to close
    PressEnter without selection to search using Google

    [8]ページ先頭

    ©2009-2025 Movatter.jp