The syntactic-sugar form isn't there with the @, but it can still be done in a single line.
https://coderpad.io/RWTYEFY3
<?php
// DEFINE DECORATOR
$decoratorFunction = function($orig) {
print "decorating $orig";
return function() use ($orig) {
$params = func_get_args();
print "\nCalling a function with: " . count($params) . " arguments";
return call_user_func_array($orig, $params);
};
};
// DEFINE A FUNCTION TO DECORATE
function someFunction($a, $b) {
return $a + $b;
}
// CREATE NEW DECORATED FUNCTION(s)
$decorated = $decoratorFunction("someFunction");
$decorated2 = $decoratorFunction("printf");
echo "\noutput: " . $decorated(1,2);
echo "\nOutput: " . $decorated2("\n%s", "cool");
}
https://coderpad.io/RWTYEFY3
<?php
// DEFINE DECORATOR
$decoratorFunction = function($orig) { print "decorating $orig"; return function() use ($orig) { $params = func_get_args(); print "\nCalling a function with: " . count($params) . " arguments"; return call_user_func_array($orig, $params); }; };
// DEFINE A FUNCTION TO DECORATE
function someFunction($a, $b) { return $a + $b; }
// CREATE NEW DECORATED FUNCTION(s)
$decorated = $decoratorFunction("someFunction");
$decorated2 = $decoratorFunction("printf");
echo "\noutput: " . $decorated(1,2);
echo "\nOutput: " . $decorated2("\n%s", "cool"); }