Anonymous function with dynamic number of arguments/parameters - php

So, some time ago I build a parser for jQuery Query Builder plugin, which parses the formula into PHP code that returns some calculation based on added parameters, it could range from return $a + $b; to something like
if($a == 'some_value' || $c == 'other_value') {
return $something;
} else if($b == 'something' && $d == 'anything') {
return $something_else;
} else {
return $anything;
}
and it could be even more complex. The thing is that it creates this as a string, which I then passed to another function which returns a dynamic function created with create_function, but that constructor in PHP is deprecated as of version 7.2.0.
My problem now is that I need to be able to create anonymous function with dynamic number of parameters, and those parameters need to have dynamic variable names. Here is my previous code
protected function createFunction($formula, &$data)
{
$args = '';
foreach($data as $key => $value) {
$args .= '$' . $key . ', ';
}
return create_function(substr($args, 0, strlen($args) - 2), $formula);
}
As you can see, the $formula is that dynamic PHP code I wrote above, and $data is an associative array (usually a row from database). Any ideas?
Edit: Forgot to mention, the formula itself is not the problem, as I can just use eval() function for that (I'm not concerned with security here so it's ok), it's just that I'm not sure how to add dynamic number of parameters with dynamic variable names.

You may go with Anonymous functions with this.
I had used eval in this case due to your comment :
Edit: Forgot to mention, the formula itself is not the problem, as I
can just use eval() function for that (I'm not concerned with security
here so it's ok)
class Foo
{
public function createFunction($formula, $args)
{
$func = function ($args) use ($formula) {
foreach ($args as $key => $val) {
$$key = $val;
}
return eval($formula);
};
return $func($args);
}
}
$foo = new Foo;
$foo->createFunction('echo $a + $b;', ['a' => 1, 'b' => 2]);
a live sample for your code
https://3v4l.org/HrMKN

Related

How to write a function that could be called like func(a)(b)(c) in php?

I need to realize function "calc" that works like that:
$sum = function($a, $b) { return $a + $b; };
calc(5)(3)(2)($sum); // 10
calc(1)(2)($sum); // 3
calc(2)(3)('pow'); // 8
I can write something like this:
function calc(){;
print_r(func_get_args());
return __FUNCTION__;
}
calc(3)(5)(2)('sum');
and it print Array ( [0] => 3 ) Array ( [0] => 5 ) Array ( [0] => 2 ) Array ( [0] => sum ).
So, when I get 'sum' in my function, i should have an array with all previous arguments.
But i have no idea, how can i pass current argument in next function call to manipulate all of them on last iteration. Or is there some sort of recursive solution?
What you're talking about is called Currying. The following code will require PHP 7, since it involves invoking a function returned from another one, which wasn't possible until PHP's Abstract Syntax Tree was implemented in that version.
First things first, you'll need a new sum() function that can operate on an arbitrary number of variables:
$sum = function(...$args) { return array_sum($args); };
Secondly, the important part. A function that returns a new anonymous function, accumulating the arguments as it goes. When you finally pass it something callable (either your $sum function, or a built-in function name like pow), it'll execute it, unpacking the arguments that it's built up.
function calc($x)
{
return function($y = null) use ($x)
{
if (is_callable($y)) {
return $y(...$x);
} else {
$args = (array) $x;
$args[] = $y;
return calc($args);
}
};
}
echo calc(5)(3)(2)($sum); // 10
echo calc(1)(2)($sum); // 3
echo calc(2)(3)('pow'); // 8
See https://3v4l.org/r0emm
(Note that internal functions will be limited to operating on the number of arguments they are defined to take - calc(2)(3)(4)('pow') will raise an error.)
This isn't a particularly common pattern to use (which is probably why you've found it hard to track down), so please for everyone who reads it's sake, think carefully about where you use it.
Credit to the curryAdd answer in this question for the starting blocks.
Edit: I stand corrected, you don't require globals it seems! Definitely use the #iainn's answer over this one.
So to achieve this you're going to have to use globals if you're not doing it within a class to maintain current state. You can see a working example of the below code here (note that it only works for PHP version 7 and above)
<?php
$sum = function(...$args) {
return array_sum($args);
};
function calc(...$args) {
global $globalArguments;
if (is_callable($args[0])) {
$callback = $args[0];
$arguments = array_map(function ($arg) {
return $arg[0];
}, $globalArguments);
return $callback(...$arguments);
}
$globalArguments[] = $args;
return __FUNCTION__;
}
echo calc(3)(2)($sum); // 5
I don't know why you want to do this, but I don't suggest it in production, globals aren't something that should really be used if you can avoid it.
function calc(int $value, Callable $function = null)
{
return function ($v) use ($value, $function) {
$f = function ($call) use ($value, $function) {
return (is_callable($call) && is_callable($function)) ? $call($function($call), $value) : $value;
};
return is_callable($v) ? $f($v) : calc($v, $f);
};
}

Is it possible to make a PHP function that would take any number of arguments?

I am writing some PHP code that would generate HTML files from templates.
I would like, if possible, to make a function that would take any strings I feed the function with, and put that into the file. Like so:
function generator($a, $b, $c, $n...){
$filename = $a . ".html";
ob_start ();
echo $b;
echo $c;
echo $d;
echo $n...;
$buffer = ob_get_clean();
file_put_contents($a, $buffer);
}
I need this, because different pages would have different number of include files, and with this I would be able to skip making different functions for specific pages. Just an iterator, and that's it.
Thanks!
From PHP 5.6+ you can use ... to indicate a variable number of arguments:
function test (... $args)
{
foreach ($args as $arg) {
echo $arg;
}
}
test("testing", "variable"); // testing variable
Demo
Variable-length argument lists from the manual
So, your function would look something like this:
function generator($a, $b, $c, ... $n) {
$filename = $a . ".html";
ob_start();
echo $b;
echo $c;
foreach ($n as $var) {
echo $var;
}
$buffer = ob_get_clean();
file_put_contents($a, $buffer);
}
You can also use variadic functions (PHP 5.6+) :
function generator($a, ...$args) {
echo $a . "\n";
print_r($args);
}
generator("test", 1, 2, 3, 4);
Outputs :
"test"
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
)
You can make it using an array as following :
function generator($array){
// set the first item of the array as name of the .html file and take it out of the array.
$filename = array_shift($array) . ".html";
ob_start ();
// echo all the array fields
foreach($array as $a){
echo $a;
}
$buffer = ob_get_clean();
file_put_contents($a, $buffer);
}
You can pass the array directly to call the function like the following :
generator( ["val_1", "val_2", "val_3"] );
Just use func_get_args(); inside your function to return an array of all arguments passed in.
You can also use func_get_arg($arg_num) to return a specific argument, or func_num_args to return the number of arguments.
All PHP functions allow any number of parameters, they just won't be callable by name, the only way is with these 3 functions.
Note, you may use a variadic argument as the last in the parameter list like so:
function my_func($x,$y, ... $z){
//Now $z is an array of all arguments after the first two
}
In the process of good design, I would think carefully about when and where to use things such as this. For example I currently work on a project that probably has over 200K lines of code and for better of worse this is actually never used.
The most common way is to pass an array "struct" to the method:
$args = array();
$args['kitchen'] = 'sink';
$args['bath'] = 'room';
$args['cat'] = array('fur','tail');
$func->someFunction($args);
If you wanted to have more control over the data you could create a struct and access that within the class. Public functions act as handlers.
class SomeClass {
....
private $args
public function setArgs($arg1,$arg2,$arg3) {
$this->arg1 = $arg1;
...
}
public function getArgs() {
return $this->args;
}
More rarely you can have C++ like control where you use a class just as a struct:
class MyStruct {
public $foo;
public $bar;
private $secret;
private function getSecret() {
return $secret;
}
protect function setSecret($val) {
$secret = $val;
}
}
Already mentioned is '...' which I nearly never see but it's interesting, though how useful ? Does this help explain what is going on?
function someFunction(... $args)
Usually you will see a mix of things in methods which helps articulate the purpose of it.
private function someSmallFunc($list = array(), $val = '', $limit = 10)
This example is to illustrate the natural grouping of information, data is in a list, $val is used for something to control the method along with $limit say limits the number of query results. Hence, you should think in this way about your methods IMO.
Also if you notice default values are set ($limit = 10) to in case they aren't passed in. For example if you call someSmallFunc($data, $someVal) (opposed to say someSmallFunc($data, $someVal, 20) ) and not pass in $limit it will default to 10.

What is the good example of using 'func_get_arg' in PHP?

I just have found out that there is a function called func_get_arg in PHP which enables developer to use variant style of getting arguments.
It seems to be very useful because number of argument can now be arbitrary, but I cannot think of any good example of using it.
What are the some examples of using this function to fully benefit its polymorphic characteristic?
I usually use func_get_args() which is easier to use if wanting multiple arguments.
For example, to recreate PHP's max().
function max() {
$max = -PHP_INT_MAX;
foreach(func_get_args() as $arg) {
if ($arg > $max) {
$max = $arg;
}
}
return $max;
}
CodePad.
Now you can do echo max(1,5,7,3) and get 7.
As of php5.6, there isn't a use case of func_get_arg anymore; its functionality has been replaced by the variadic function using the ... syntax:
/**
* #param array $arguments
*/
public function poit(...$arguments)
{
foreach($arguments as $argument) {
...
}
}
This is especially useful if there are methods that are overloaded at the end; one does need to filter out the first arguments anymore, as showcased by an example:
Old style using func_get_arg:
function foo($a, $b) {
$c = array();
if (func_num_args() > 2) {
for($i = 0; $i < func_num_args()-2; $i++) {
$c[] = func_get_arg($i+2);
}
}
var_dump($a, $b, $c)
// Do something
}
foo('a', 'b', 'c', 'd');
Newer variadic style;
function foo($a, $b, …$c) {
var_dump($a, $b, $c);
// Do something
}
foo('a', 'b', 'c', 'd');
Both foo produce the same output, one is much simpler to write.
First of all, you are using the term "polymorphism" totally wrong. Polymorphism is a concept in object-oriented programming, and it has nothing to do with variable number of arguments in functions.
In my experience, all func_get_args allows you to do is add a little syntactic sugar.
Think of a function that can take any number of integers and return their sum. (I 'm cheating, as this already exists in array_sum. But cheating is good if it keeps the example simple). You could do it this way:
// you can leave "array" out; I have it because we should be getting one here
function sum1(array $integers) {
return array_sum($integers);
}
Now you would call this like so:
$sum = sum1(array(1));
$sum = sum1(array(1, 2, 3, 4));
This isn't very pretty. But we can do better:
function sum2() {
$integers = func_get_args();
return array_sum($integers);
}
Now you can call it like this:
$sum = sum2(1);
$sum = sum2(1, 2, 3, 4);
Let's say we have multiple arrays containing data in which we need to search across the keys for their values without merging these arrays.
The arrays are like:
$a = array('a' => 5, 'b' => 6);
$b = array('a' => 2, 'b' => 8);
$c = array('a' => 7, 'b' => 3);
In that case, say we need to get all the values of the key a from all the arrays. We can write a function that take in arbitrary number of arrays to search in.
// we need the key, and at least 1 array to search in
function simpleSearchArrays($key, $a1){
$arrays = func_get_args();
array_shift($arrays); // remove the first argument, which is the key
$ret = array();
foreach($arrays as $a){
if(array_key_exists($key, $a)){
$ret[] = $a[$key];
}
}
return $ret;
}
So if we use the function:
$x = simpleSearchArrays('a', $a, $b, $c);
$x will then contain array(5, 2, 7).
Personally, I don't think there is a good use case for it inside a normal function. As a control freak I like to know exactly what is being passed to my functions and I like to know exactly what I'm passing.
However, it can be use full for things like Dynamic/Static URL routing. When you are rewriting (via mod_rewrite) the URL args to a single bootstrap.
In this sense, you can have arguments that don't necessarily need to exist with every page request.
I hardly ever use func_get_arg(), but I do use its cousin func_get_args() quite a bit. Here's one example, a function along the lines of the echo statement that entity encodes all its arguments:
function ee() {
$args = func_get_args();
echo implode('', array_map('htmlentities', $args));
}
I use that function quite a bit.
Here's another useful example, a function that does the same job as SQL's COALESCE() function.
function coalesce() {
$args = func_get_args();
foreach ($args as $arg) {
if (!is_null($arg)) {
return $arg;
}
}
return null;
}
It returns the first non-null argument passed in, or null if there's no such argument.
Gets an array of the function's argument list.
This function may be used in conjunction with func_get_arg() and func_num_args() to allow user-defined functions to accept variable-length argument lists.
<?php
function foo()
{
$numargs = func_num_args();
echo "Number of arguments: $numargs \n";
if ($numargs >= 2) {
echo "Second argument is: " . func_get_arg(1) . "\n";
}
$arg_list = func_get_args();
for ($i = 0; $i < $numargs; $i++) {
echo "Argument $i is: " . $arg_list[$i] . "\n";
}
}
foo(1, 2, 3);
?>

Defining a function with name from a variable?

I need to create a column-system for Wordpress with shortcodes, which is not a problem, but I'm trying to make it with less code.
I have an array with the data needed, I loop through it, create a unique-named function and set it as shortcode-function. The third step is a mystery. How can I create a function from a variable.
Here's an example, how it should be done:
$data[] = "first";
$data[] = "second";
foreach($data as $key => $value) {
function $value($atts,$content) {
return '<div class="'.$value.'">'.$content.'</div>';
}
add_shortcode($value,$value);
}
However, it seems that it's not possible to make it work like that in PHP. Is there any way to make this work, as I would not want to write all the (identical) functions separated. I could make the shortcode something like [col first]text[/col] but the client wants to have different names for every one of them.
you can use the double dollar syntax to use the value of a variable as a variable identifier,
Example:
$variable = "test";
$$variable = "value of test"
echo $test; //or echo $$variable;
I have never tried but you way want to try:
foreach($data as $key => $value)
{
function $$value($atts,$content)
{
}
add_shortcode($value,$value);
}
or a function like create_function
if your using PHP 5.3 or greater then you can do something like so:
$$value = function()
{
}
which should work fine
I'm not sure how WP invocates the functions, but if it uses call_user_func then you might cheat by using an object with virtual methods:
class fake_functions {
function __call($name, $params) {
return '<div class="'.$name.'">'.$params[1].'</div>';
}
}
$obj = new fake_functions();
foreach ($data as $value) {
add_shortcode($value, array($obj,$value));
}

php: array_replace_recursive alternative

I need a solution for array_replace_recursive, because my php-version isn't high enough. I want to use this code:
$_GET = array_replace_recursive($_GET, array("__amp__"=>"&"));
easy, isn't it?
On the PHP docs page for array_replace_recursive, someone posted the following source code to use in place of it:
<?php
if (!function_exists('array_replace_recursive'))
{
function array_replace_recursive($array, $array1)
{
function recurse($array, $array1)
{
foreach ($array1 as $key => $value)
{
// create new key in $array, if it is empty or not an array
if (!isset($array[$key]) || (isset($array[$key]) && !is_array($array[$key])))
{
$array[$key] = array();
}
// overwrite the value in the base array
if (is_array($value))
{
$value = recurse($array[$key], $value);
}
$array[$key] = $value;
}
return $array;
}
// handle the arguments, merge one by one
$args = func_get_args();
$array = $args[0];
if (!is_array($array))
{
return $array;
}
for ($i = 1; $i < count($args); $i++)
{
if (is_array($args[$i]))
{
$array = recurse($array, $args[$i]);
}
}
return $array;
}
}
?>
The code above by #Justin is ok, save for 2 things:
Function is not readily available at start of php execution be cause it is wrapped in if(). PHP docu says
When a function is defined in a conditional manner such as the two examples shown. Its definition must be processed prior to being called.
Most importantly; calling the function twice results in fatal error.
PHP docu says
All functions and classes in PHP have the global scope - they can be called outside a function even if they were defined inside and vice versa.
So I just moved the recurse function outside array_replace_recursive function and it worked well. I also removed the if() condition and renamed it to array_replace_recursive_b4php53 for fear of future upgradings

Categories