oop - php Closure: what to do next with it? -


i have simple example:

function func1(){     return function (){         return 1;     }; } 

but it's not clear me how can use reason. seems func1() returns same anonymous function does. if write this:

echo func1(); 

...i error:

catchable fatal error: object of class closure not converted string in [file_name] on line [line] 

i can nothing except closure object:

var_dump(func1()); --------------------- object(closure)[1] 

however can't see practical use of this. contains same object no matter anonymous function returns - object(closure)[1]. (i use php 5.3.3) see no way store value anonymous function returns within func1(). see closuer object anyway. how use there , beyond?

in example, return value of call func1 function (more specifically, closure). error you're getting due php not being able convert object string. if want print value returned closure, need call it.

function func1() {     return function () {         return 1;     }; }  $f = func1(); // create closure echo $f();    // calls closure 

this example function returning function , doesn't demonstrate sets closure apart other first class function, , closure can contain state unique it's creation. in other words, can generate functions same code differ in data can access.

consider trivial example:

 function multiplier($m) {      return function ($v) use ($m) {          return $v * $m;      };  }   $mult_5 = multiplier(5);  echo $mult_5(5); // prints 25   $mult_10 = multiplier(10);  echo $mult_10(5); // prints 50 

again, very trivial example, demonstrate couple of important things. first, defined single function, yet calling function able generate 2 similar different functions changing parameters when called them. also, consider function each have own "state". in case of function named $mult_5, knows own internal $m value 5, , different $m value of $mult_10 function. value each of these passed multiplier function , function has completed, value lives on in returned function/closure.

it's worth noting each return value of call multiplier first class function, meaning can write generalized functions (like multiplier) , use them generate more specific functions 'on-the-fly', functions more appropriate current environment/state of program.

if familiar oop, above example re-written using oop:

class multiplier {          protected $m;          public function __construct($m) {                 $this->m = $m;         }          public function multiply($v) {                 return $v * $this->m;         } }  $mult_5 = new multiplier(5); echo $mult_5->multiply(5);  // prints 25  $mult_10 = new multiplier(10); echo $mult_10->multiply(5); // prints 50 

... and, subjective, prefer more consice syntax of closure.

alternatively, use more general function begin with:

function multiplier($a, $b) {     return $a * $b; }  echo multiplier(5, 5); echo multiplier(5, 10); 

but benefit of using closure instead can hide data (like multiplier, in example).


Comments

Popular posts from this blog

matlab - Deleting rows with specific rules -

php - MySQLi multi_query results for later use -