Check for anonymous functions in PHP arrays? - php

How can we check for anonymous functions inside PHP arrays?
Example:
$array = array('callback' => function() {
die('calls back');
});
Can we then just simply use in_array, and to something like this:
if( in_array(function() {}, $array) ) {
// Yes! There is an anonymous function inside my elements.
} else {
// Nop! There are no anonymous function inside of me.
}
I'm experimenting with method chaining and PHP's Magic Methods, and I've come to the point where I provide some functions anonymously, and just want to check if they are defined, but I wish not to loop through the object, nor to use gettype, or anything similar.

You can filter the array by checking if the value is an instance of Closure:
$array = array( 'callback' => function() { die( 'callback'); });
$anon_fns = array_filter( $array, function( $el) { return $el instanceof Closure; });
if( count( $anon_fns) == 0) { // Assumes count( $array) > 0
echo 'No anonymous functions in the array';
} else {
echo 'Anonymous functions exist in the array';
}
Pretty much, just check if the element of the array is an instance of Closure. If it is, you have a callable type.

Nickb's answer is great for figuring out if it is an anonymous function, but you may also use is_callable to figure out if it is any type of function ( probably more safe to assume )
For example
$x = function() { die(); }
$response = action( array( $x ) );
...
public function action( $array ){
foreach( $array as $element )
if( is_callable( $element ) )
....
}

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);
};
}

PHP - Passing functions with arguments as arguments

I have several interchangeable functions with different numbers of arguments, for example:
function doSomething1($arg1) {
…
}
function doSomething2($arg1, $arg2) {
…
}
I would like to pass a certain number of these functions, complete with arguments, to another handling function, such as:
function doTwoThings($thing1, $thing2) {
$thing1();
$thing2();
}
Obviously this syntax is not correct but I think it gets my point across. The handling function would be called something like this:
doTwoThings(‘doSomething1(‘abc’)’, ‘doSomething2(‘abc’, ‘123’));
So the question is, how is this actually done?
From my research it sounds like I may be able to "wrap" the "doSomething" function calls in an anonymous function, complete with arguments and pass those "wrapped" functions to the "doTwoThings" function, and since the anonymous function doesn't technically have arguments they could be called in the fashion shown above in the second code snippet. The PHP documentation has me confused and none of the examples I'm finding put everything together. Any help would be greatly appreciated!
you could make use of call_user_func_array() which takes a callback (eg a function or class method to run) and the arguments as an array.
http://php.net/manual/en/function.call-user-func-array.php
The func_get_args() means you can feed this funciton and arbitary number of arguments.
http://php.net/manual/en/function.func-get-args.php
domanythings(
array( 'thingonename', array('thing','one','arguments') ),
array( 'thingtwoname', array('thing','two','arguments') )
);
funciton domanythings()
{
$results = array();
foreach( func_get_args() as $thing )
{
// $thing[0] = 'thingonename';
// $thing[1] = array('thing','one','arguments')
if( is_array( $thing ) === true and isset( $thing[0] ) and is_callable( $thing[0] ) )
{
if( isset( $thing[1] ) and is_array( $thing[1] ) )
{
$results[] = call_user_func_array( $thing[0], $thing[1] );
}
else
{
$results[] = call_user_func( $thing[0] );
}
}
else
{
throw new Exception( 'Invalid thing' );
}
}
return $results;
}
This would be the same as doing
thingonename('thing','one','arguments');
thingtwoname('thing','two','arguments');

Iterate over an array of classes and push certain properties into an array

Here is the code that will iterate over an array of classes, and push the content_id property of each element (if it exists) into an array:
# Collect content jobs ids from the job to process
$jobsToProcessContentIds = [];
foreach ( $jobsToProcess as $job ) {
if ( $job->content_id ?? null ) {
array_push( $jobsToProcessContentIds, $job->content_id );
}
}
Is there a shorter, more declarative way to achieve this?
PHP code demo
<?php
class x
{
public $content_id="y";
}
class y
{
public $content="z";
}
$jobsToProcess=array(new x(), new y());
$jobsToProcessContentIds=array();
foreach ($jobsToProcess as $job)
{
if (property_exists($job, "content_id"))
{
$jobsToProcessContentIds[]=$job->content_id;
}
}
print_r($jobsToProcessContentIds);
Output:
Array
(
[0] => y
)
This may not be much shorter but it seems a good choice for a declarative approach is to use array_reduce().
$jobsToProcessContentIds = array_reduce($jobsToProcess, function($carry, $job) {
if ($job->content_id ?? null) {
$carry[] = $job->content_id;
}
return $carry;
});
Two lines could be saved by using the short-circuiting logical AND operator (i.e. &&), though some would argue it is less readable.
$jobsToProcessContentIds = array_reduce($jobsToProcess, function($carry, $job) {
($job->content_id ?? null) && $carry[] = $job->content_id;
return $carry;
});
See it demonstrated in this phpfiddle.

Check if the variables passed to a function is empty

Im working on a register function that will register users into the database.
I want a checker in that function that checks if any of the arguments are empty. I've simplified the problem, so this is not the retail look.
<?php
create_user($_POST["username"], $_POST["epost"]);
function create_user($username, $epost){
// Pull all the arguments in create_user and check if they are empty
// Instead of doing this:
if(empty($username) || empty($epost)){
}
}
Reason for making this is so i can simply add another argument to the function and it checks automatically that it isnt empty.
Shorted question:
How do I check if all the arguments in a function isnt empty?
function create_user($username, $epost){
foreach(func_get_args() as $arg)
{
//.. check the arg
}
}
You can use array_filter and array_map functions also.
For example create a function like below
<?php
function isEmpty( $items, $length ) {
$items = array_map( "trim", $items );
$items = array_filter( $items );
return ( count( $items ) !== (int)$length );
}
?>
the above function accepts two parameters.
$items = array of arguments,
$length = the number of arguments the function accepts.
you can use it like below
<?php
create_user( $_POST["username"], $_POST["epost"] );
function create_user( $username, $epost ) {
if ( isEmpty( func_get_args(), 2 ) ) {
// some arguments are empty
}
}
?>

Pass extra parameters to usort callback

I have the following functions. WordPress functions, but this is really a PHP question. They sort my $term objects according to the artist_lastname property in each object's metadata.
I want to pass a string into $meta in the first function. This would let me reuse this code as I could apply it to various metadata properties.
But I don't understand how I can pass extra parameters to the usort callback. I tried to make a JS style anonymous function but the PHP version on the server is too old (v. 5.2.17) and threw a syntax error.
Any help - or a shove towards the right corner of the manual - gratefully appreciated. Thanks!
function sort_by_term_meta($terms, $meta)
{
usort($terms,"term_meta_cmp");
}
function term_meta_cmp( $a, $b )
{
$name_a = get_term_meta($a->term_id, 'artist_lastname', true);
$name_b = get_term_meta($b->term_id, 'artist_lastname', true);
return strcmp($name_a, $name_b);
}
PHP Version: 5.2.17
I think this question deserves an update. I know the original question was for PHP version 5.2, but I came here looking for a solution and found one for newer versions of PHP and thought this might be useful for other people as well.
For PHP 5.3 and up, you can use the 'use' keyword to introduce local variables into the local scope of an anonymous function. So the following should work:
function sort_by_term_meta(&$terms, $meta) {
usort($terms, function($a, $b) use ($meta) {
$name_a = get_term_meta($a->term_id, 'artist_lastname', true);
$name_b = get_term_meta($b->term_id, 'artist_lastname', true);
return strcmp($name_a, $name_b);
});
}
Some more general code
If you want to sort an array just once and need an extra argument you can use an anonymous function like this:
usort($arrayToSort, function($a, $b) use ($myExtraArgument) {
//$myExtraArgument is available in this scope
//perform sorting, return -1, 0, 1
return strcmp($a, $b);
});
If you need a reusable function to sort an array which needs an extra argument, you can always wrap the anonymous function, like for the original question:
function mySortFunction(&$arrayToSort, $myExtraArgument1, $myExtraArgument2) {
usort($arrayToSort, function($a, $b) use ($myExtraArgument1, $myExtraArgument2) {
//$myExtraArgument1 and 2 are available in this scope
//perform sorting, return -1, 0, 1
return strcmp($a, $b);
});
}
In PHP, one option for a callback is to pass a two-element array containing an object handle and a method name to call on the object. For example, if $obj was an instance of class MyCallable, and you want to call the method1 method of MyCallable on $obj, then you can pass array($obj, "method1") as a callback.
One solution using this supported callback type is to define a single-use class that essentially acts like a closure type:
function sort_by_term_meta( $terms, $meta )
{
usort($terms, array(new TermMetaCmpClosure($meta), "call"));
}
function term_meta_cmp( $a, $b, $meta )
{
$name_a = get_term_meta($a->term_id, $meta, true);
$name_b = get_term_meta($b->term_id, $meta, true);
return strcmp($name_a, $name_b);
}
class TermMetaCmpClosure
{
private $meta;
function __construct( $meta ) {
$this->meta = $meta;
}
function call( $a, $b ) {
return term_meta_cmp($a, $b, $this->meta);
}
}
Assuming you've access to objects and static (PHP 5 or greater), you can create an object and pass the arguments directly there, like so:
<?php
class SortWithMeta {
private static $meta;
static function sort(&$terms, $meta) {
self::$meta = $meta;
usort($terms, array("SortWithMeta", "cmp_method"));
}
static function cmp_method($a, $b) {
$meta = self::$meta; //access meta data
// do comparison here
}
}
// then call it
SortWithMeta::sort($terms, array('hello'));
Assuming you don't have access to objects/static; you could just do a global:
$meta = array('hello'); //define meta in global
function term_meta_cmp($a, $b) {
global $meta; //access meta data
// do comparison here
}
usort($terms, 'term_meta_cmp');
Warning
This function has been DEPRECATED as of PHP 7.2.0. Relying on this function is highly discouraged.
The docs say that create_function() should work on PHP >= 4.0.1. Does this work?
function term_meta_cmp( $a, $b, $meta ) {
echo "$a, $b, $meta<hr>"; // Debugging output
}
$terms = array("d","c","b","a");
usort($terms, create_function('$a, $b', 'return term_meta_cmp($a, $b, "some-meta");'));
This won't help you at all with usort() but might be helpful nevertheless. You could sort the array using one of the other sorting functions, array_multisort().
The idea is to build an array of the values that you would be sorting on (the return values from get_term_meta()) and multisort that against your main $terms array.
function sort_by_term_meta(&$terms, $meta)
{
$sort_on = array();
foreach ($terms as $term) {
$sort_on[] = get_term_meta($term->term_id, $meta, true);
}
array_multisort($sort_on, SORT_ASC, SORT_STRING, $terms);
}
What is the Simplest Solution to Passing Args to usort()?
I like many of the answers here, but I wanted to have a solution that could be done as simply as possible, but could also be demonstrated! When calling usort, supply extra arguments like this...
usort($sortable, [$arg1, $arg2, ... $argn, compareFunction]);
But make sure to define these arguments before, so, you'll end up with something like...
$arg1 = 'something';
$arg2 = 'something else';
$argn = 'yet another thing';
usort($sortable, [$arg1, $arg2, ... $argn, compareFunction]);
Then $arg1, $arg2, and $argn will be available to the compareFunction().
Demo It Up!
To demonstrate, here is a usort() that only considers the first three letters of elements being compared...
function cmp ($a, $b) {
return strcmp(substr($a, 0, $num), substr($a, 0, $num));
}
$terms = ['123a', '123z', '123b',];
$num = 3;
$thing = 4;
usort($terms, [$num, $thing, cmp]);
print_r($terms);
Full Working Demo Online

Categories