Martin Kruliš by Martin Kruliš (v1.0)1
Interpreted Language ◦ No pointers ◦ Dynamic data structures usually depend on pointers Instead of Pointers ◦ The arrays are flexible enough ◦ Objects are passed by reference (not by value) What Else Is There ◦ Variable variables ◦ References by Martin Kruliš (v1.0)2
Indirect Access to Values ◦ Name of one variable is stored in another variable $a = 'b'; $$a = 42; // the same as $b = 42; $a = 'b'; $b = 'c'; $c = 'd'; $$$$a = 'hello'; // the same as $d = 'hello'; ◦ The {, } can be used to avoid ambiguous situations ◦ Can be used with members, functions, classes, … $obj = new $className(); $obj->$varName = 42; by Martin Kruliš (v1.0)3
References ◦ Similar to Unix hard-links in FS ◦ Multiple variables attached to the same data ◦ Independent on object references A reference to an object can be created $a = 1; $b = &$a; $b++; echo $a; // prints by Martin Kruliš (v1.0)4 int (1) $a $b int (2)
Arguments as References ◦ Similar usage as var keyword in Pascal function inc(&$x) { $x++; } Returning References function &findIt($what) { global $myArray; return &$myArray[$what]; } by Martin Kruliš (v1.0)5
References vs. Pointers function foo(&$var) { $var = &$GLOBALS['bar']; } The unset() Function ◦ Does not remove data, only the variable ◦ Data are removed when not referenced The global Declaration global $a; $a = &$GLOBALS['a']; by Martin Kruliš (v1.0)6
Declaration ◦ Keyword function followed by the identifier function foo([args, …]) { … body … } Function Body ◦ Pretty much anything (even a function/class decl.) Returning Results ◦ Optional argument of the return construct ◦ Only one value, but it can be an array by Martin Kruliš (v1.0)7
Argument Declarations ◦ Implicit values may be provided function foo($x, $y = 1, $z = 2) { … } Arguments with implicit values are aligned to the right Note that PHP functions does not support overloading Variable Number of Arguments ◦ Any function can be called with more arguments than formally declared ◦ Functions func_num_args(), func_get_arg(), and func_get_args() provide access to such arguments by Martin Kruliš (v1.0)8
Indirect Calling ◦ Calling a function by its name stored in a variable function foo($x, $y) { … } $funcName = 'foo'; $funcName(42, 54);// the same as foo(42, 54); Similar Constructs ◦ Using specialized invocation functions call_user_func('foo', 42, 54); call_user_func_array('foo', array(42, 54)); by Martin Kruliš (v1.0)9
Testing Function Existence ◦ function_exists() – test whether given func. exists ◦ get_defined_functions() – list of all func. names Cleanup Functions ◦ register_shutdown_function() – registers a function, which is executed when the script finishes Special Case of Left-side Function ◦ Func. list() is used at the left-side of assignments Reverse logic – it fills its arguments list($a, $b, $c) = array(1, 2, 3); by Martin Kruliš (v1.0)10
Lambda (Nameless) Functions ◦ A unique name is generated automatically ◦ Function create_function() Gets the arguments and the body as strings Returns an identifier of newly created function ◦ Useful in many situations One-call functions Call-back functions $mul = create_function('$x, $y', 'return $x * $y'); echo $mul(3, 4);// prints out '12' by Martin Kruliš (v1.0)11
Anonymous Functions ◦ New way how to implement nameless functions $fnc = function($args) { …body… }; ◦ The anonymous function is an instance of Closure It can be passed on like an object ◦ The visible variables must be explicitly stated $fnc = function(…) use ($var, &$refvar) { … }; These variables are captured in the closure Variables passed by reference can be modified by Martin Kruliš (v1.0)12 Example 1
by Martin Kruliš (v1.0)13