Primi 0.0.15
-
New: Support binding native PHP functions as Primi variables and/or functions
- More of a "backend" feature.
- Those who want to use Primi as library now can predefine functions that are to be available during the execution. These can be passed in as ordinary PHP closures (PHP callables will be internally converted to PHP closures).
- New class
FunctionContainer
now acts as a universal wrapper for both standard Primi functions and also for newly supported native PHP functions. Note: Functions created from PHP callables won't have access to the caller context, but passing arguments from user-land to predefined native functions is fully supported. - Arguments are passed as Primi value objects (eg.
\Smuuf\Primi\Structures\StringValue
). A Primi value object is then also expected as the return value.
-
New: Array value now has 4 new methods:
-
Change: Invoked functions now capture the context of where it was defined. Previous behavior was that the context of the caller was passed into the function.
Example of binding native PHP functions:
use \Smuuf\Primi\Context;
use \Smuuf\Primi\Structures\FunctionContainer;
use \Smuuf\Primi\Structures\StringValue;
use \Smuuf\Primi\Structures\FuncValue;
$c = new Context;
// Build function container from a native PHP anonymous function...
$fnContainer = FunctionContainer::buildNative(function($a, $b) {
return new StringValue($a->getInternalValue() . "_" . $b->getInternalValue())
});
// Store the function value in the context as variable.
$c->setVariable('fun_fun', new FuncValue($fnContainer)));
// At this point the function "fun_fun" (which expects two arguments and returns a string)
// is available in user-land...