How to get the parameters of a closure in php

Asked

Viewed 22 times

1

How could I pick up the parameters of a closure?
I was doing some tests and I gave one var_dump in a closure and she returned me that

object(Closure)#5 (1) {
  ["parameter"]=> 
    array(2) {
    ["$val"]=>
    string(10) ""
    ["$val2"]=>
    string(10) ""
  }
}

And I got the doubt, how I access the array 'paramenter'?

var_dump("<pre>", function($val, $val2){ });
  • if you can put the complete code?

  • var_dump("<pre>", function($val, $val2){&#xA;});

  • places the code in the question by editing the question

1 answer

1


To get the parameters of an anonymous function can be done with Reflectionfunction, example:

<?php

$fs = function($val, $val2){ return $val + $val2; };

$reflection = new ReflectionFunction($fs);
$arguments  = $reflection->getParameters();
var_dump($arguments);

and on his departure from command var_dump:

array(2) {
  [0]=>
  object(ReflectionParameter)#3 (1) {
    ["name"]=>
    string(3) "val"
  }
  [1]=>
  object(ReflectionParameter)#4 (1) {
    ["name"]=>
    string(4) "val2"
  }
}

Reference: Deducing PHP Closure Parameters

Browser other questions tagged

You are not signed in. Login or sign up in order to post.