Anonymous Functions

An anonymous function is simply a function with no name.

Anonymous functions, also known as closures, allow the creation of functions which have no specified name. They are most useful as the value of callback parameters, but they have many other uses.

Anonymous functions are implemented using the Closure class.

  • Anonymous functions can be bound to objects
  • Methods bind() and bindTo() of the Closure object provide means to create closures with different binding and scope
  • Binding defines the value of $this and the scope for a closure

An anonymous function has no name so you would define it like this

// Anonymous function
 function () {
 return "PHP Codez";
 }

Anonymous function variable assignment

<?php
$greet = function($name)
{
 printf("Hello %s\r\n", $name);
};

$greet('Codez');
$greet('PHP');
?>
Share

Leave a Reply

Your email address will not be published. Required fields are marked *