The PHP anonymous functions are the functions that have no names. So there is no identifier for the function.

Usually the PHP anonymous functions can be written without names like the below example.

function( $param ) {
  ... Statements
}

That means there is no calling for this function. Let’s see more examples.

Basic Examples for Anonymous Function

The anonymous function can be called in predefined function such as usort, uksort, etc. for example.

<?php
   $array = array_map( function( $field ){
      $field = $field * 2;
      return $field;
   }, array( "item1" => 10, "item2" => 50, "item3" => 1000 ));
   print_r( $array ); // => Array ( [item1] => 20 [item2] => 100 [item3] => 2000
?>

Also it can be stored with variable.

<?php
   $var = function() {
     echo "Hello World";
   }
?>

And when you call it you have to use the variable as a function pattern.

<?php
   $var();
?>

Inheriting Variables from Variables Scope in PHP Anonymous Function

<?php
   $value = "Result ...";
   $result = function() {
     echo $value; // it doesn't work
   };
   $result();
?>

Note that: In case you are using a variable scope and need to use it within anonymous function you wouldn’t able to call it directly, you have to write the defined word use between the function head and body.

The true syntax would be like the below.

<?php
   $value = "Result ...";
   $result = function() use ($value) {
     echo $value;  
   };
   $result(); // Result ...
?>

Also, you can use the use word to pass the value by reference.

<?php
  
  $print = "CodedTag Tutorials";
  $func = function() use ( &$print ) {
    echo $print;
  };

  $func();

?>

The anonymous function accepts arguments, for example.

<?php
  
  $print = "Welcome to our %s Tutorials";
  $func = function($site_name) use( $print ) {
    echo sprintf($print, $site_name );
  };

  $func( "CodedTag" ); // Welcome to our CodedTag Tutorials
  
?>

If you already using the OOP and you need to use the PHP anonymous function, you have to follow the below code.

<?php
  
  class Car {
    public $speed;
    public function automate( $speed ) {
      return function() use ( $speed )  {
        $this->speed = $speed;
      };
    }
  }
   
  $obj = new Car ();
  $anonymous = $obj->automate( 120 );
  $anonymous();
  echo $obj->speed; // 120

?>

Passing an Anonymous Function as a Callback Argument

PHP already has more than 1000 predefined functions and there are many functions accepting a function as an argument such as array_map, array_filter, usort and so many else.

Let’s see an example.

<?php

  $array     = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 ];

  
  /* => Anonymous function stored as a variable 
        to pass it into the callback */
  $anonymous = function( $first, $second ) {

    if ( $first == $secon ) 
      return 0;

    if ( $first < $second ) return 1;
    
    return -1;

  };

  usort( $array, $anonymous );

  echo "<pre>";
  print_r( $array );
  echo "</pre>";

This example will print an array from the biggest number to the smallest number.

Array
(
    [0] => 11
    [1] => 10
    [2] => 9
    [3] => 8
    [4] => 7
    [5] => 6
    [6] => 5
    [7] => 4
    [8] => 3
    [9] => 2
    [10] => 1
)

Wrapping Up

In this article you leaned about PHP anonymous function and how it does work with OOP. To learn more details visit PHP manual.