get function parameter with default value

user2907171

i know how to get function parameters.

I am using of course ReflectionMethod to get function parameters but currently i am not able to get default value of those parameter.

this is my code where function is defined

class A{
    public function fn($para1,$para2="not required"){
            //some kind of stuff
    }
}

code for getting function parameter

$meth = new ReflectionMethod("A","fn");
foreach ($meth->getParameters() as $param) {
    //some kind of stuff   
}

now please tell how do i get default value of function parameter

steven

From the docs:

<?php
function foo($test, $bar = 'baz')
{
    echo $test . $bar;
}

$function = new ReflectionFunction('foo');

foreach ($function->getParameters() as $param) {
    echo 'Name: ' . $param->getName() . PHP_EOL;
    if ($param->isOptional()) {
        echo 'Default value: ' . $param->getDefaultValue() . PHP_EOL;
    }
    echo PHP_EOL;
}
?>

http://www.php.net/manual/en/reflectionparameter.getdefaultvalue.php

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Default value for function as parameter

JavaScript function default parameter value

Reflection to get default value of parameter

Which default value to use for an optional parameter that is a function

Set the default value for the key of the function object parameter

Default value for parameter of Function type in Dart

Accommodating empy function parameter without default value

Kotlin default generic parameter of function return value

Difficulties to assign default value to a parameter of a function

Default function parameter value in safari is not working

Set a default parameter value for a JavaScript function

Accessing a function's default parameter value in Kotlin

Default value for optional generic parameter in Swift function

default value for template function, functional parameter

How to change default value of optional function parameter

Mock default parameter value in inner function

Default parameter value in template function, depending on type

Default value of function parameter initialized by list initialization

Omitting parameter with default value in a subclass function override

Using function for default value of parameter in constructor in PHP

Passing function as default value of an optional parameter

Get Value from parameter function

Is there a way in PowerShell to get the default value of a script parameter?

Get default value of stored procedure parameter

Why can't use let to declare a variable using the same name as one function parameter, even the parameter get set default value?

Sending default parameter as null and use default value set in function

Using the length of a parameter array as the default value of another parameter of the same function

python function: scope of function parameter initialized with default value

Default value in function parameter retained from previous function call