Índice

Function Basics

<?php
function name( $arg1, $arg2, … $arg_n ) { }
name( $arg1, $arg2, … $arg_m ) { }   // when calling the function, $arg_m needs not be all defined in the function header
$a = name;  $a($arg1, $arg2);    // variable function
<?php
function printList($string, $count = 5){ } // default values must be put to the right
function printList($count=5, $string){ } is invalid
function newTo5(&$number = 2){ } // as PHP 5, default value may be assigned to reference
function fun($x, $x=1, $x=2){ return $x;}; echo fun(3); // gives 2, as arguments are evaluated from left to right
function fun($x){ return $x;}; echo fun(3,4,5,6,7,8); // gives 3, warning will be issued only when fewer arguments are supplied than defined in the function header
// inside a function, cannot be put in an inc file alone from PHP 5.3 (scope warning), used for variable-length argument list which is allowed for any function in PHP
$a = func_get_args ();  // all arguments in an array
int func_num_args (void)
mixed func_get_arg ( int $arg_num ) // 0, 1, … if func_get_arg(10) will return 0 if 10 not exists

Return

By reference

<?php
function &name() { return $someref; }
$newref = & name();

Variable Functions

$func = 'foo'; $func('test');  // This calls foo('test');
$foo = new Foo(); $func='variable'; $foo->$func(); // This calls $foo->variable();

Anonymous Functions (Lambda Functions) and Closures

PHP allows the creation of functions which have no specified name (disposable 1 time use only). They are most useful as the value of callback parameters, but they have many other uses. Anonymous functions can also be used as the values of variables.

<?php
$greet = function($name)
{
printf("Hello %s\r\n", $name);
}; // the trailing semicolon is NECESSARY
$greet('World');
$greet('PHP');
?>

It is possible to use func_num_args(), func_get_arg(), and func_get_args() from within the anonymous function. Use is_callable($ab) to test whether $ab is an anonymous function.
The create_function() function is used to create an anonymous function in PHP. This function call creates a new randomly named function and returns its name (as a string).

$foo = create_function('$x,$y', 'return $x*$y;'); // must be single quoted or escaped double quoted, otherwise variables will be evaluated
echo $foo(10); (echo $foo will give lambda_1)

A closure is any function which closes over the environment in which it was defined. This means that it can access variables not in its parameter list. 

The predefined final class Closure was introduced in PHP 5.3.0. It is used for internal implementation of anonymous functions. The class has a constructor forbidding the manual creation of the object (issues E_RECOVERABLE_ERROR) and the __invoke method with the calling magic. The closure can inherit variables from the parent scope.

<?php
$callback = // defined within the class Cart and the public function getTotal($tax)
function ($quantity, $product) use ($tax, &$total) // access variables from the parent
{
$pricePerItem = constant(__CLASS__ . "::PRICE_" .strtoupper($product));
$total += ($pricePerItem * $quantity) * ($tax + 1.0);
};
array_walk($this->products, $callback);