Search code examples
phpanonymous-function

Binding an anonymous function to an existing function in PHP


Is there a way to bind an existing function to an anonymous one in php? Something like

$my_func = strip_tags();

Or must I half-redefine it, as a sort of anonymous wrapper, with the proper arguments and return value?

I tried googling this, but I suppose I didn't correctly suss the proper search phrase, as I didn't find results on the first page.

Edit I'm making a sort of function pipeline(?) where I can pass data and functions, and I want to pass functions as variables. I would like to keep the syntax the same and be able to use $output = $function($data) without having to write a bunch of anonymous wrappings for native functions. Also I would like to avoid using call_user_func so I don't have to re-write my existing code.


Solution

  • Simple.

    $my_func = strip_tags(...); # string 'strip_tags' before PHP 8.1.0
    $output = $my_func($data);