get closure's parameters class type in php

lets consider this closure

$a = function (class_name $var){
  //
};

now I want to get get class_name before calling the function like var_dump($a->parameter::class) but its not working
so is there any way to access this class_name ?

edit:
this function can receive various type of classes so I want to know the class name and then include it.

Upvotes: 0

Views: 366

Answers (1)

Jeto
Jeto

Reputation: 14927

You may use reflection, and especially ReflectionFunction to access the parameter type:

class Foo {}
$a = function (Foo $var) {
  //
};

$function = new ReflectionFunction($a);
$parameter = $function->getParameters()[0];
$parameterType = $parameter->getType();

echo $parameterType->getName();  // Foo

Demo

Upvotes: 1

Related Questions