Created
January 1, 2017 13:11
-
-
Save juliomatcom/3426c0a0a75f1e589c86945454844c49 to your computer and use it in GitHub Desktop.
Functional Programming example in PHP - 1
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
// Functional Programming example in PHP | |
<?php | |
function myFoo($foo){ | |
return function ($bar) use ($foo){ | |
return "Foo is ${foo} and Bar is ${bar}"; | |
}; | |
} | |
//first call set $foo = 'Asd' | |
$myBarAsd = myFoo('Asd'); | |
//second call sets $bar = '123' | |
$result_123 = $myBarAsd('123'); | |
//second call sets $bar = '456' | |
$result_456 = $myBarAsd('456'); | |
//use the return function without passing the data again (and it should be noted, you | |
//can't set the data again) | |
var_dump($result_123); | |
//string(25) "Foo is Asd and Bar is 123" | |
var_dump($result_456); | |
//string(25) "Foo is Asd and Bar is 456" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment