Created
December 27, 2010 17:37
-
-
Save shameerc/756333 to your computer and use it in GitHub Desktop.
Basic example for create_function()
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| $array = array(1,2,3,4,5); | |
| $v= 4; | |
| $result = array_filter($array,function($n) use(&$v){return $v === $n;}); | |
| print_r($reslt); | |
| ?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| //Bad closure. | |
| $fact = function($n) { | |
| if($n>=1) | |
| return 1; | |
| else | |
| return $n*$fact(n-1); | |
| } | |
| echo $fact(5); // Output : Error | |
| ?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| $factorial = function($n) use (&$factorial) { | |
| if ($n <= 1) | |
| return 1; | |
| else | |
| return $n * $factorial($n - 1); | |
| }; | |
| echo $factorial(5); //Output : 120 | |
| ?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| $sum = create_function('$a,$b','return $a+$b;'); | |
| echo $sum(2,3); //Output : 5 | |
| ?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var fact = function(n) { | |
| if(n <= 1) | |
| return 1; | |
| else | |
| return n*fact(n-1); | |
| }; | |
| alert(fact(5)) // Output : 120 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| //Classic Y-combinator function | |
| function Y($F) { | |
| $func = function ($f) { return $f($f); }; | |
| return $func(function ($f) use($F) { | |
| return $F(function ($x) use($f) { | |
| $ff = $f($f); | |
| return $ff($x); | |
| }); | |
| }); | |
| } | |
| //Anonymous factorial function using Y-combinator | |
| $factorial = Y(function($fact){ | |
| return function($n) use($fact) { | |
| if($n<=1) | |
| return 1; | |
| else | |
| return $n*$fact($n-1); | |
| }; | |
| }); | |
| echo $factorial(5); //Output : 120 | |
| ?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| //Simple math class | |
| class Math | |
| { | |
| function __construct($a) { | |
| $this->a =$a; | |
| } | |
| //Returns a closure | |
| function mul() | |
| { | |
| //We can't directly use $this inside closure | |
| $self = $this; | |
| return function($n) use($self) { | |
| return $n*$self->a; | |
| }; | |
| } | |
| } | |
| $math = new Math(5); | |
| $mul = $math->mul(); | |
| echo $mul(4); //Output : 20 | |
| ?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| //This class demonstrate a way of prototyping | |
| //applications using closures | |
| Class Hello | |
| { | |
| public $name; | |
| public $methods; | |
| Public function __call($method, $args) | |
| { | |
| return call_user_func_array($this->methods[$method], $args); | |
| } | |
| public function __set($name, $value) | |
| { | |
| $this->methods[$name] = $value ; | |
| } | |
| } | |
| $hello= new Hello(); | |
| $hello->name = 'User'; | |
| $hello->greet = function() use ($hello) { | |
| return 'Hello '.$hello->name; | |
| }; | |
| echo $hello->greet(); //Output : Hello User | |
| ?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| $sum = function($a,$b) { | |
| return $a+$b; | |
| }; | |
| echo $sum(2,3); //Output : 5 | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In
array_filter_closure.phpyou have a typo.$resltshould be$result.