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);
?>
Related
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);
};
}
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
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.
From this question here, I was writing an enum wrapper to have some methods that can be used with lambdas to somewhat emulate ruby's usage of blocks in enums.
class enum {
public $arr;
function __construct($array) {
$this->arr = $array;
}
function each($lambda) {
array_walk($this->arr, $lambda);
}
function find_all($lambda) {
return array_filter($this->arr, $lambda);
}
function inject($lambda, $initial=null) {
if ($initial == null) {
$first = array_shift($this->arr);
$result = array_reduce($this->arr, $lambda, $first);
array_unshift($this->arr, $first);
return $result;
} else {
return array_reduce($this->arr, $lambda, $initial);
}
}
}
$list = new enum(array(-1, 3, 4, 5, -7));
$list->each(function($a) { print $a . "\n";});
// in PHP you can also assign a closure to a variable
$pos = function($a) { return ($a < 0) ? false : true;};
$positives = $list->find_all($pos);
Now, how could I implement inject() as elegantly as possible?
EDIT: method implemented as seen above. Usage examples:
// inject() examples
$list = new enum(range(5, 10));
$sum = $list->inject(function($sum, $n) { return $sum+$n; });
$product = $list->inject(function($acc, $n) { return $acc*$n; }, 1);
$list = new enum(array('cat', 'sheep', 'bear'));
$longest = $list->inject(function($memo, $word) {
return (strlen($memo) > strlen($word)) ? $memo : $word; }
);
I'm not familiar with Ruby, but from the description, it seems similar to array_reduce.
mixed array_reduce ( array $input , callback $function [, mixed $initial = NULL ] )
array_reduce() applies iteratively the function function to the elements of the array input, so as to reduce the array to a single value.
In addition to "reduce", this operation is also sometimes called "fold"; in Mathematica:
Fold[f, init, {a, b, c, d}] == f[f[f[f[init, a], b], c], d]
The second form uses the first element of the collection as a the initial value (and skips that element while iterating).
This second form can be implemented this way:
//$arr is the initial array
$first = array_shift($arr);
$result = array_reduce($arr, $callback, $first);
Response to Mladen
The array functions in PHP cannot be used that way because they can only work with arrays, not arbitrary objects.
There are a few options here:
You could convert the object into an array prior to passing it to array_reduce. In practice, this doesn't have much value because the conversion consists of creating an array with the object properties as elements. This behavior can only be changed internally (writing a native extension).
You could have all your objects implement an interface with a method toArray that would have to be called priorly to passing it to array_reduce. Not a great idea, either.
You could implement a version of array_reduce that works with any Traversable object. This would be easy to do, but you couldn't put a Traversable type hint in the function declaration since arrays are not objects. With such a hint, every array would have to be encapsulated in an ArrayIterator object prior to the function call.
How can I use PHP 5.3 Closures like We use Blocks in Ruby.
I never used 'for' Loop in Ruby due to using Blocks with 'each' 'find_all' 'inject' Methods.
How can I use PHP 5.3 Closures like Ruby Blocks and say bye-bye to 'for' Loops :)
Like Between { and } is a Closure(or Block or Anonymous Function)
fruit = %w[apple banana orange]
fruit.each { |f| print "#{f}, " }
I do it in PHP this way,
$fruit = array('apple', 'banana', 'orange');
foreach ($fruit as $f)
{
print "$f, ";
}
Is there a way to do this the Ruby way using PHP Closures as PHP 5.3 supports it.
If you are looking at using lambdas to iterate over a PHP array, there are certain functions that you could use to accomplish that. Better illustrate it, I used a wrapper class enum:
class enum {
public $arr;
function __construct($array) {
$this->arr = $array;
}
function each($lambda) {
array_walk($this->arr, $lambda);
}
function find_all($lambda) {
return array_filter($this->arr, $lambda);
}
function inject($lambda, $initial=null) {
if ($initial == null) {
$first = array_shift($this->arr);
$result = array_reduce($this->arr, $lambda, $first);
array_unshift($this->arr, $first);
return $result;
} else {
return array_reduce($this->arr, $lambda, $initial);
}
}
}
$list = new enum(array(-1, 3, 4, 5, -7));
$list->each(function($a) { print $a . "\n";});
// in PHP you can also assign a closure to a variable
$pos = function($a) { return ($a < 0) ? false : true;};
$positives = $list->find_all($pos);
// inject() examples
$list = new enum(range(5, 10));
$sum = $list->inject(function($sum, $n) { return $sum+$n; });
$product = $list->inject(function($acc, $n) { return $acc*$n; }, 1);
$list = new enum(array('cat', 'sheep', 'bear'));
$longest = $list->inject(function($memo, $word) {
return (strlen($memo) > strlen($word)) ? $memo : $word; }
);
That being said, closures in PHP aren't meant to replace the for loop nor do they behave like ruby blocks.
I think array_map() and array_walk() look better as RubyBlocks replacement.
I don't think an anonymous function is a substitute for a for loop, nor do I think it's necessary to replace for loops with them.
What it is useful for is a callback. Take this for example:
(yes, it is a lame bubble sort, but it is an example)
<?php
function bubble_sort($sort_rule, $elements) {
do {
$swapped = false;
for ($i = 0; $i < count($elements) - 1; $i++) {
if ($sort_rule($elements[$i], $elements[$i + 1])) {
$elements[$i] ^= $elements[$i + 1];
$elements[$i + 1] ^= $elements[$i];
$elements[$i] ^= $elements[$i + 1];
$swapped = true;
}
}
} while($swapped);
return $elements;
}
print_r(bubble_sort(function ($a, $b) { if ($a > $b) return true; else return false; }
,array(1,6,3,7,42,-1,0,6)));
?>
Closures aren't a replacement for for loops in a procedural programming language like php. Sure if you're using lisp or scheme they are, but that's out of necessity.
You can write them that way, all you'll really be doing is creating an anonymous function with a for loop inside it. I think recursion would just be unnecessary if the task is just as easily accomplished with a for loop, and therefore you're not kissing for loops good bye.
Anonymous functions are very useful in event driven programming as well, when you want to just define a callback method really quick.
Simple answer: you don't. Ruby is not devoid of for() loops, they just mask the word "for" and change the syntax a bit. If you wanted to use a closure, it'd just be a closure with a loop inside it, or an ugly (and less efficient) recursive closure.
And closures are NOT the same thing as blocks. Closures are comparable to functions in JavaScript-- that is, they can be stored in variables and sent as arguments.