mercredi 3 août 2022

Building a function from an arbitrary callable with a matching reflection in PHP

Lets say I have an arbitrary function taking some parameters and returning say an array (the return and what the method does doesn't matter)

$callable = fn (MyClass $myClass, string $id, bool $rec) => []

I would like to build a new function from this closure so that this new function takes the exact same parameters in a way that the Reflection of this function will return the same parameters as the original

What I mean:

$clonedCallable = someMagic($callable);
assertEquals(// Equal but not the same ref obviously
  (new ReflectionFunction($callable))->getParameters(),
  (new ReflectionFunction($clonedCallable))->getParameters()
);

Is there a way in PHP to build this kind of method without using some hackjob eval (which is the only idea I have right now) as Reflection doesn't have some kind of magic setParameters()?





Aucun commentaire:

Enregistrer un commentaire