Download presentation
Presentation is loading. Please wait.
Published byBlaze Barker Modified over 9 years ago
1
Martin Kruliš 26. 2. 2015 by Martin Kruliš (v1.0)1
2
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 26. 2. 2015 by Martin Kruliš (v1.0)2
3
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; 26. 2. 2015 by Martin Kruliš (v1.0)3
4
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 2 26. 2. 2015 by Martin Kruliš (v1.0)4 int (1) $a $b int (2)
5
Arguments as References ◦ Similar usage as var keyword in Pascal function inc(&$x) { $x++; } Returning References function &findIt($what) { global $myArray; return &$myArray[$what]; } 26. 2. 2015 by Martin Kruliš (v1.0)5
6
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']; 26. 2. 2015 by Martin Kruliš (v1.0)6
7
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 26. 2. 2015 by Martin Kruliš (v1.0)7
8
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 26. 2. 2015 by Martin Kruliš (v1.0)8
9
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)); 26. 2. 2015 by Martin Kruliš (v1.0)9
10
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); 26. 2. 2015 by Martin Kruliš (v1.0)10
11
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' 26. 2. 2015 by Martin Kruliš (v1.0)11
12
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 26. 2. 2015 by Martin Kruliš (v1.0)12 Example 1
13
26. 2. 2015 by Martin Kruliš (v1.0)13
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.