Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save juliomatcom/3426c0a0a75f1e589c86945454844c49 to your computer and use it in GitHub Desktop.
Save juliomatcom/3426c0a0a75f1e589c86945454844c49 to your computer and use it in GitHub Desktop.
Functional Programming example in PHP - 1
// 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