SlideShare a Scribd company logo
ELEMENTS OF

FUNCTIONAL PROGRAMMING

IN PHP
JAREK JAKUBOWSKI
PYTHON DEVELOPER
SCALA ENTHUSIAST
PHP DEVELOPER
/jarek.jakubowski
FUNCTIONAL PHP
WHAT IS FUNCTIONAL PROGRAMMING?
1. Functions as primary citizens:

- save them as variables

- pass them as arguments

- return them from another 

function
2. Lambda calculus:

- closures (PHP >= 5.3)

- generators (PHP >= 5.5)
FUNCTIONAL PHP
WHAT IS FUNCTIONAL PROGRAMMING?
$functionAsVariable = function (callable $functionAsArgument): callable {
return function () use ($functionAsArgument) {
return $functionAsArgument();
};
};
$foo = 'bar';
return $functionAsVariable(function () use ($foo): string {
return $foo;
})();
FUNCTIONAL PHP
YOU HAVE PROBABLY USED IT ALREADY
const functionAsVariable = arg => functionAsArgument => functionAsArgument(arg);
let foo = 'bar';
return functionAsVariable(foo)( (arg) => arg );
Modern JS (ES6):
FUNCTIONAL PHP
YOU HAVE PROBABLY USED IT ALREADY
$.ajax({
url: "demo_test.txt",
success: function (result) {
$("#div1").html(result);
},
error: function (xhr) {
alert("An error occured: " + xhr.status + " " + xhr.statusText);
}
});
Old JS (jQuery):
FUNCTIONAL PHP
YOU CAN USE IT ALREADY
use ReactPromisePromise;
(new Promise(file_get_contents('foo.txt')))
->then(function (string $result): string {
echo 'well done! '.$result;
})
->otherwise(function (Throwable $error) {
throw $error;
});
Adequate code in PHP:
FUNCTIONAL PHP
PROS (PHP)
▸ LESS ERRORS

YOU HAVE TO HANDLE EVERY CASE

Fatal error: Call to a member function on null
▸ METHOD CHAINING
▸ CONCURRENT (ASYNC) CODE
▸ IMMUTABILITY
▸ ES / DDD MADE EASY
FUNCTIONAL PHP
CONS (PHP)
▸ LESS READABLE CODE 

(PHP SYNTAX DOESN’T FIT BEST)
▸ NOT OBVIOUS FOR PHP DEVS

(while JS Devs use it everyday)
▸ PERFORMANCE

(but do we really care about it?)
TOOLS
FUNCTIONAL PHP
PhpSlang is a PHP library aiming to fill the gaps 

between PHP and classical functional languages.
PHPSLANG
REACTPHP
ReactPHP is a low-level library for event-driven programming in PHP.
phpslang.io
reactphp.org
TOOLS
FUNCTIONAL PHP
Asynchronous & Fault-tolerant PHP Framework 

for Distributed Applications.
KRAKEN FRAMEWORK
IMMUTABLE.PHP
Immutable collections, with filter, map, join, sort, slice, and other methods. 

Well-suited for functional programming and memory-intensive applications. 

Runs especially fast in PHP7.
https://github.com/jkoudys/immutable.php
kraken-php.com
HOW?
FUNCTIONAL PHP
1. Forget about null checks
2. Forget about throwing Exceptions
3. Forget about variables
4. Forget about loops
5. Use async calls wherever you can
6. Avoid mutating the State
HOW?
FUNCTIONAL PHP
1. Forget about null checks











HOW?
public function displayLocalNumber(string $email): string
{
$user = $this->userRepository->findByEmail($email);
if (
! is_null($user)
&& ! is_null($user->getProfile())
&& ! is_null($user->getProfile()->getAddress())
&& ! is_null($user->getProfile()->getAddress()->getLocalNumber())
) {
return 'Local number is: ' .
$user->getProfile()->getAddress()->getLocalNumber();
} else {
return 'No sufficient data';
}
}
FUNCTIONAL PHP
1. Forget about null checks
HOW?
public function displayLocalNumber(string $email): string
{
$user = $this->userRepository->findByEmail($email);
if (null !== $user) {
if (null !== $profile = $user->getProfile()) {
if (null !== $address = $profile->getAddress()) {
if (null !== $localNumber = $address->getLocalNumber()) {
return 'Local number is: ' . $localNumber;
}
}
}
}
return 'No sufficient data';
}
FUNCTIONAL PHP
1. Forget about null checks
HOW?
use PhpSlangOptionOption;
public function displayLocalNumber(string $email): string
{
return Option::of($this->userRepository->findByEmail($email))
->flatMap(function(User $user): Option {
return Option::of($user->getProfile());
})
->flatMap(function(Profile $profile): Option {
return Option::of($profile->getAddress());
})
->flatMap(function(Address $address): Option {
return Option::of($address->getLocalNumber());
})
->map(function(int $localNumber): string {
return 'Local number is: ' . $localNumber;
})
->getOrElse('No sufficient data');
}
FUNCTIONAL PHP
1. Forget about null checks

Use Option instead (Option can contain nothing or something)
HOW?
use PhpSlangOptionOption;
public function displayLocalNumber(string $email): string
{
return $this->userRepository->findByEmail($email)
->flatMap(function(User $user): Option {
return $user->getProfile();
})
->flatMap(function(Profile $profile): Option {
return $profile->getAddress();
})
->flatMap(function(Address $address): Option {
return $address->getLocalNumber();
})
->map(function(int $localNumber): string {
return 'Local number is: ' . $localNumber;
})
->getOrElse('No sufficient data');
}
FUNCTIONAL PHP
1. Forget about null checks

Return Option instead (Option can contain nothing or something)
OPTION
FUNCTIONAL PHP
Option can contain nothing or something
class Some extends Option {}
class None extends Option {}
/**
* @var Option<Some, None>
*/
OPTION
FUNCTIONAL PHP
1. Static constructor Option::of(mixed $value)
2. Map methods:
1. map(function(mixed $value): mixed {}): Option

Option<string> => string

Option<Option<string>> => Option<string>
2. flatMap(function(mixed $value): Option {}): Option

Option<string> => string

Option<Option<string>> => string
3. Return methods:
1. getOrElse(mixed $returnWhenNull): mixed
2. getOrCall(callable $callWhenNull): mixed
OPTION
FUNCTIONAL PHP
Best practices:
1. Return Option whenever function might return null
2. Use map(Closure $expression) when $expression returns unpacked value

map(function(): mixed): Option
3. Use flatMap(Closure $expression) when $expression returns value packed
in Option

flatMap(function(): Option): Option
4. Don’t forget to call getOrCall() / getOrElse() at end! - only at this point all
defined expressions are evaluated
MAP() VS FLATMAP()
use PhpSlangOptionOption;
public function getUser(string $email): Option<User>
{
return Option::of($this->userRepository->findByEmail($email));
}
public function getUserNameOption(string $email): Option<string>
{
return $this->getUser($email)
->map(function (User $user): Option {
return Option::of($user->getName());
})
->getOrElse(new Some($email));
}
public function getUserNameFlat(string $email): string
{
return $this->getUser($email)
->flatMap(function (User $user): Option {
return Option::of($user->getName());
})
->getOrElse($email);
}
FUNCTIONAL PHP
HOW?
FUNCTIONAL PHP
1. Forget about null checks
2. Forget about throwing Exceptions
HOW?
public function getUserName(string $email): ?string
{
try {
return $this->findUser($email)->getName();
} catch (InvalidArgumentException $e) {
return null;
}
}
private function findUser(string $email): User
{
$user = $this->userRepository->findBy(['email' => $email]);
if (!$user instanceof User) {
throw new InvalidArgumentException('User not found.');
}
return $user;
}
FUNCTIONAL PHP
2. Forget about throwing Exceptions
HOW?
public function getUserName(string $email): ?string
{
try {
return $this->findUser($email)->getName();
} catch (InvalidArgumentException $e) {
return null;
}
}
private function findUser(string $email): User
{
$user = $this->userRepository->findBy(['email' => $email]);
if (!$user instanceof User) {
throw new InvalidArgumentException('User not found.');
}
return $user;
}
FUNCTIONAL PHP 2. Forget about throwing Exceptions
HOW?
public function getUserName(string $email): Option
{
return $this->findUser($email)
->right(function (User $user): Some {
return new Some($user->getName());
})
->left(function (): None {
return new None();
})
->get();
}
private function findUser(string $email): Either
{
return Option::of($this->userRepository->findBy(['email' => $email]))
->map(function (User $user) {
return new Right($user);
})
->getOrElse(function () {
return new Left('User not found.');
});
}
FUNCTIONAL PHP 2. Forget about throwing Exceptions: Use Either instead
EITHER
FUNCTIONAL PHP
abstract class Either
{
abstract public function left(Closure $expression): Either;
abstract public function right(Closure $expression): Either;
abstract public function flatLeft(Closure $expression): Either;
abstract public function flatRight(Closure $expression): Either;
public function get(): mixed {};
}

class Right extends Either {}
class Left extends Either {}
EITHER
FUNCTIONAL PHP
Best practices:
1. Use Right of expected value, Left for unexpected/error
2. Use left(Closure $expression) / right(Closure $expression) when $expression returns unpacked value

right(function(): mixed): Either
3. Use flatLeft(Closure $expression) / flatRight(Closure $expression) when $expression returns value
packed in either Right or Left

flatRight(function(): Either): Either
4. flatRight() for chaining, right() on success, left() on error
5. Don’t forget to call get() at end! - only at this point all defined expressions are evaluated
HOW?
FUNCTIONAL PHP
1. Forget about null checks
2. Forget about throwing Exceptions
3. Forget about variables
HOW?
public function getUserName(string $email): Option
{
return $this->findUser($email)
->flatRight(function (User $user) {
return new Some($user->getName());
})
->flatLeft(function () {
return new None();
})
->get();
}
private function getUser(string $email): Either
{
return Option::of($this->userRepository->findBy(['email' => $email]))
->map(function (User $user) {
return new Right($user);
})
->getOrElse(function () {
return new Left('User not found.');
});
}
FUNCTIONAL PHP
3. Forget about variables

Avoid declaring them (I’ve already done it)
HOW?
public function getUserName(string $email): Option
{
return $this->findUser($email)
->flatRight(function (User $user) {
return new Some($user->getName());
})
->flatLeft(function () {
return new None();
})
->get();
}
private function getUser(string $email): Either
{
return Option::of($this->userRepository->findBy(['email' => $email]))
->map(function (User $user) {
return new Right($user);
})
->getOrElse(function () {
return new Left('User not found.');
});
}
FUNCTIONAL PHP 3. Forget about variables: Avoid declaring them (I’ve already done it)
HOW?
FUNCTIONAL PHP
1. Forget about null checks
2. Forget about throwing Exceptions
3. Forget about variables
4. Forget about loops

HOW?
$data = [1,2,3,4,5];
$result = [];
foreach ($data as $key => $value) {
$result[$key] = $value * 2;
}
$data = array_map(function (int $value): int {
return $value * 2;
}, $data);
$data = array_filter($data, function (int $value): bool {
return $value % 2 === 0;
});
$data = array_reduce($data, function (int $prev, int $value): int {
return $prev + $value;
}, 0);
return $data;
FUNCTIONAL PHP
4. Forget about loops
HOW?
use PhpSlangCollectionParallelListCollection;
return ParallelListCollection::of([1,2,3,4,5])
->map(function (int $value): int {
return $value * 2;
})
->filter(function (int $value): bool {
return $value % 2 === 0;
})
->fold(0, function (int $prev, int $value): int {
return $prev + $value;
});
FUNCTIONAL PHP
4. Forget about loops

Use Collection instead
COLLECTIONS
FUNCTIONAL PHP
Libraries which provide them:
1. Doctrine collections (incomplete functionality)
2. PhpSlang
3. Immutable.php
Collections provide unified API for PHP’s array_* functions and sometimes they are even faster than
native PHP.



Methods:
1. filter() (array_filter)
2. map() (array_map)
3. reduce() / fold() (array_reduce)
4. sort() (usort)
HOW?
FUNCTIONAL PHP
1. Forget about null checks
2. Forget about throwing Exceptions
3. Forget about variables
4. Forget about loops
5. Use async calls wherever you can
HOW?
use function ClueReactBlockawaitAll;
/* use try-catch because ReactPHP's awaitAll uses Exceptions to escape loop
and PhpSlang's Future monad is not ready yet */
try {
$argumentsArray = awaitAll([
$this->getStatus($valuesArray[‘status']),
$this->getIdea($valuesArray['idea']),
$this->getUserFromToken(),
$valuesArray['reason'] ?? null
], LoopFactory::create());
return new Right($argumentsArray);
} catch (NotFoundHttpException $e) {
return new Left($e);
}
FUNCTIONAL PHP
5. Use async calls wherever you can
HOW?
FUNCTIONAL PHP
1. Forget about null checks
2. Forget about loops
3. Forget about throwing Exceptions
4. Forget about variables
5. Use async calls wherever you can
6. Avoid mutating the State
$today = new DateTime();
$yesterday = $today->modify('-1 day');
echo "Yesterday was {$yesterday->format('l')}
and today is {$today->format('l')}";
FUNCTIONAL PHP
HOW? 6. Avoid mutating the State
$today = new DateTime();
$yesterday = $today->modify('-1 day');
echo "Yesterday was {$yesterday->format('l')}
and today is {$today->format('l')}";
// prints "Yesterday was Thursday and today is Thursday"
FUNCTIONAL PHP
HOW? 6. Avoid mutating the State
$today = new DateTimeImmutable();
$yesterday = $today->modify('-1 day');
echo "Yesterday was {$yesterday->format('l')}
and today is {$today->format(‘l')}";
// prints "Yesterday was Thursday and today is Friday"
FUNCTIONAL PHP
HOW? 6. Avoid mutating the State
Use Immutable Classes
FUNCTIONAL PHP
REAL WORLD EXAMPLE
FUNCTIONAL PHP
HOW IS IT CALLED IN OTHER LANGUAGES?
FUNCTIONAL PHP
JS (ELM) PHP (PHPSLANG) SCALA (JAVASLANG)
Maybe (Just, Nothing) Option (Some, None) Option (Some, None)
Either (Right, Left) Either (Right, Left) Either (Right, Left)
Promise Promise (Future?) Future
async, await await, awaitAll Await.result
QUESTIONS ?
FUNCTIONAL PHP
THANK YOU
FURTHER READING
FUNCTIONAL PHP
▸ Functional Programming basics
▸ http://maciejpirog.github.io/fishy/
▸ http://adit.io/posts/2013-04-17-functors,_applicatives,_and_monads_in_pictures.html
▸ https://medium.com/@cscalfani/so-you-want-to-be-a-functional-programmer-part-1-1f15e387e536
▸ Missing parts of PHP (maybe in 7.2…)
▸ https://wiki.php.net/rfc/generics (https://www.bountysource.com/issues/20553561-add-generics-support)
▸ https://wiki.php.net/rfc/immutability
▸ https://wiki.php.net/rfc/pipe-operator
▸ PhpSlang http://phpslang.io/
▸ https://php-slang.github.io/php-slang-docs/static/Introduction/Thinking_Functional.html
▸ https://php-slang.github.io/php-slang-docs/static/Usage/Essentials/Option.html
▸ ReactPHP http://reactphp.org/
▸ https://github.com/clue/php-block-react

More Related Content

Similar to Elements of Functional Programming in PHP (20)

PDF
Durian: a PHP 5.5 microframework with generator-style middleware
Kuan Yen Heng
 
PDF
SPL: The Missing Link in Development
jsmith92
 
PDF
PHP 5.3 Overview
jsmith92
 
PPT
PHP 5.3 Part 2 - Lambda Functions & Closures
melechi
 
PPTX
A Functional Guide to Cat Herding with PHP Generators
Mark Baker
 
PPT
Go OO! - Real-life Design Patterns in PHP 5
Stephan Schmidt
 
PDF
08 Advanced PHP #burningkeyboards
Denis Ristic
 
PDF
関西PHP勉強会 php5.4つまみぐい
Hisateru Tanaka
 
PDF
Functional php
Jean Carlo Machado
 
PPTX
PHP Basics
Saraswathi Murugan
 
PDF
Preparing for the next PHP version (5.6)
Damien Seguy
 
PPT
Php Reusing Code And Writing Functions
mussawir20
 
PDF
PHP7 - Scalar Type Hints & Return Types
Eric Poe
 
PDF
Slide
Naing Lin Aung
 
PDF
PHP Conference Asia 2016
Britta Alex
 
PDF
Hack tutorial
Wakana Yoshizawa
 
PDF
02 - Second meetup
EdiPHP
 
PPTX
Introducing PHP Latest Updates
Iftekhar Eather
 
PDF
Functional Programming for OO Programmers (part 2)
Calvin Cheng
 
PDF
Wien15 java8
Jaanus Pöial
 
Durian: a PHP 5.5 microframework with generator-style middleware
Kuan Yen Heng
 
SPL: The Missing Link in Development
jsmith92
 
PHP 5.3 Overview
jsmith92
 
PHP 5.3 Part 2 - Lambda Functions & Closures
melechi
 
A Functional Guide to Cat Herding with PHP Generators
Mark Baker
 
Go OO! - Real-life Design Patterns in PHP 5
Stephan Schmidt
 
08 Advanced PHP #burningkeyboards
Denis Ristic
 
関西PHP勉強会 php5.4つまみぐい
Hisateru Tanaka
 
Functional php
Jean Carlo Machado
 
PHP Basics
Saraswathi Murugan
 
Preparing for the next PHP version (5.6)
Damien Seguy
 
Php Reusing Code And Writing Functions
mussawir20
 
PHP7 - Scalar Type Hints & Return Types
Eric Poe
 
PHP Conference Asia 2016
Britta Alex
 
Hack tutorial
Wakana Yoshizawa
 
02 - Second meetup
EdiPHP
 
Introducing PHP Latest Updates
Iftekhar Eather
 
Functional Programming for OO Programmers (part 2)
Calvin Cheng
 
Wien15 java8
Jaanus Pöial
 

Recently uploaded (20)

PPTX
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
PPTX
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
PPTX
2025 Winter SWAYAM NPTEL & A Student.pptx
Utsav Yagnik
 
PDF
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
PDF
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
PPTX
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
PPTX
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
PPTX
MENINGITIS: NURSING MANAGEMENT, BACTERIAL MENINGITIS, VIRAL MENINGITIS.pptx
PRADEEP ABOTHU
 
PDF
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
PDF
community health nursing question paper 2.pdf
Prince kumar
 
PPTX
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
PPTX
Neurodivergent Friendly Schools - Slides from training session
Pooky Knightsmith
 
PDF
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
PDF
The-Ever-Evolving-World-of-Science (1).pdf/7TH CLASS CURIOSITY /1ST CHAPTER/B...
Sandeep Swamy
 
PPTX
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
PPT
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
PDF
Dimensions of Societal Planning in Commonism
StefanMz
 
PPTX
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
PDF
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
PPTX
SPINA BIFIDA: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
2025 Winter SWAYAM NPTEL & A Student.pptx
Utsav Yagnik
 
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
MENINGITIS: NURSING MANAGEMENT, BACTERIAL MENINGITIS, VIRAL MENINGITIS.pptx
PRADEEP ABOTHU
 
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
community health nursing question paper 2.pdf
Prince kumar
 
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
Neurodivergent Friendly Schools - Slides from training session
Pooky Knightsmith
 
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
The-Ever-Evolving-World-of-Science (1).pdf/7TH CLASS CURIOSITY /1ST CHAPTER/B...
Sandeep Swamy
 
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
Dimensions of Societal Planning in Commonism
StefanMz
 
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
SPINA BIFIDA: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
Ad

Elements of Functional Programming in PHP

  • 2. JAREK JAKUBOWSKI PYTHON DEVELOPER SCALA ENTHUSIAST PHP DEVELOPER /jarek.jakubowski
  • 3. FUNCTIONAL PHP WHAT IS FUNCTIONAL PROGRAMMING? 1. Functions as primary citizens:
 - save them as variables
 - pass them as arguments
 - return them from another 
 function 2. Lambda calculus:
 - closures (PHP >= 5.3)
 - generators (PHP >= 5.5)
  • 4. FUNCTIONAL PHP WHAT IS FUNCTIONAL PROGRAMMING? $functionAsVariable = function (callable $functionAsArgument): callable { return function () use ($functionAsArgument) { return $functionAsArgument(); }; }; $foo = 'bar'; return $functionAsVariable(function () use ($foo): string { return $foo; })();
  • 5. FUNCTIONAL PHP YOU HAVE PROBABLY USED IT ALREADY const functionAsVariable = arg => functionAsArgument => functionAsArgument(arg); let foo = 'bar'; return functionAsVariable(foo)( (arg) => arg ); Modern JS (ES6):
  • 6. FUNCTIONAL PHP YOU HAVE PROBABLY USED IT ALREADY $.ajax({ url: "demo_test.txt", success: function (result) { $("#div1").html(result); }, error: function (xhr) { alert("An error occured: " + xhr.status + " " + xhr.statusText); } }); Old JS (jQuery):
  • 7. FUNCTIONAL PHP YOU CAN USE IT ALREADY use ReactPromisePromise; (new Promise(file_get_contents('foo.txt'))) ->then(function (string $result): string { echo 'well done! '.$result; }) ->otherwise(function (Throwable $error) { throw $error; }); Adequate code in PHP:
  • 8. FUNCTIONAL PHP PROS (PHP) ▸ LESS ERRORS
 YOU HAVE TO HANDLE EVERY CASE
 Fatal error: Call to a member function on null ▸ METHOD CHAINING ▸ CONCURRENT (ASYNC) CODE ▸ IMMUTABILITY ▸ ES / DDD MADE EASY
  • 9. FUNCTIONAL PHP CONS (PHP) ▸ LESS READABLE CODE 
 (PHP SYNTAX DOESN’T FIT BEST) ▸ NOT OBVIOUS FOR PHP DEVS
 (while JS Devs use it everyday) ▸ PERFORMANCE
 (but do we really care about it?)
  • 10. TOOLS FUNCTIONAL PHP PhpSlang is a PHP library aiming to fill the gaps 
 between PHP and classical functional languages. PHPSLANG REACTPHP ReactPHP is a low-level library for event-driven programming in PHP. phpslang.io reactphp.org
  • 11. TOOLS FUNCTIONAL PHP Asynchronous & Fault-tolerant PHP Framework 
 for Distributed Applications. KRAKEN FRAMEWORK IMMUTABLE.PHP Immutable collections, with filter, map, join, sort, slice, and other methods. 
 Well-suited for functional programming and memory-intensive applications. 
 Runs especially fast in PHP7. https://github.com/jkoudys/immutable.php kraken-php.com
  • 12. HOW? FUNCTIONAL PHP 1. Forget about null checks 2. Forget about throwing Exceptions 3. Forget about variables 4. Forget about loops 5. Use async calls wherever you can 6. Avoid mutating the State
  • 13. HOW? FUNCTIONAL PHP 1. Forget about null checks
 
 
 
 
 

  • 14. HOW? public function displayLocalNumber(string $email): string { $user = $this->userRepository->findByEmail($email); if ( ! is_null($user) && ! is_null($user->getProfile()) && ! is_null($user->getProfile()->getAddress()) && ! is_null($user->getProfile()->getAddress()->getLocalNumber()) ) { return 'Local number is: ' . $user->getProfile()->getAddress()->getLocalNumber(); } else { return 'No sufficient data'; } } FUNCTIONAL PHP 1. Forget about null checks
  • 15. HOW? public function displayLocalNumber(string $email): string { $user = $this->userRepository->findByEmail($email); if (null !== $user) { if (null !== $profile = $user->getProfile()) { if (null !== $address = $profile->getAddress()) { if (null !== $localNumber = $address->getLocalNumber()) { return 'Local number is: ' . $localNumber; } } } } return 'No sufficient data'; } FUNCTIONAL PHP 1. Forget about null checks
  • 16. HOW? use PhpSlangOptionOption; public function displayLocalNumber(string $email): string { return Option::of($this->userRepository->findByEmail($email)) ->flatMap(function(User $user): Option { return Option::of($user->getProfile()); }) ->flatMap(function(Profile $profile): Option { return Option::of($profile->getAddress()); }) ->flatMap(function(Address $address): Option { return Option::of($address->getLocalNumber()); }) ->map(function(int $localNumber): string { return 'Local number is: ' . $localNumber; }) ->getOrElse('No sufficient data'); } FUNCTIONAL PHP 1. Forget about null checks
 Use Option instead (Option can contain nothing or something)
  • 17. HOW? use PhpSlangOptionOption; public function displayLocalNumber(string $email): string { return $this->userRepository->findByEmail($email) ->flatMap(function(User $user): Option { return $user->getProfile(); }) ->flatMap(function(Profile $profile): Option { return $profile->getAddress(); }) ->flatMap(function(Address $address): Option { return $address->getLocalNumber(); }) ->map(function(int $localNumber): string { return 'Local number is: ' . $localNumber; }) ->getOrElse('No sufficient data'); } FUNCTIONAL PHP 1. Forget about null checks
 Return Option instead (Option can contain nothing or something)
  • 18. OPTION FUNCTIONAL PHP Option can contain nothing or something class Some extends Option {} class None extends Option {} /** * @var Option<Some, None> */
  • 19. OPTION FUNCTIONAL PHP 1. Static constructor Option::of(mixed $value) 2. Map methods: 1. map(function(mixed $value): mixed {}): Option
 Option<string> => string
 Option<Option<string>> => Option<string> 2. flatMap(function(mixed $value): Option {}): Option
 Option<string> => string
 Option<Option<string>> => string 3. Return methods: 1. getOrElse(mixed $returnWhenNull): mixed 2. getOrCall(callable $callWhenNull): mixed
  • 20. OPTION FUNCTIONAL PHP Best practices: 1. Return Option whenever function might return null 2. Use map(Closure $expression) when $expression returns unpacked value
 map(function(): mixed): Option 3. Use flatMap(Closure $expression) when $expression returns value packed in Option
 flatMap(function(): Option): Option 4. Don’t forget to call getOrCall() / getOrElse() at end! - only at this point all defined expressions are evaluated
  • 21. MAP() VS FLATMAP() use PhpSlangOptionOption; public function getUser(string $email): Option<User> { return Option::of($this->userRepository->findByEmail($email)); } public function getUserNameOption(string $email): Option<string> { return $this->getUser($email) ->map(function (User $user): Option { return Option::of($user->getName()); }) ->getOrElse(new Some($email)); } public function getUserNameFlat(string $email): string { return $this->getUser($email) ->flatMap(function (User $user): Option { return Option::of($user->getName()); }) ->getOrElse($email); } FUNCTIONAL PHP
  • 22. HOW? FUNCTIONAL PHP 1. Forget about null checks 2. Forget about throwing Exceptions
  • 23. HOW? public function getUserName(string $email): ?string { try { return $this->findUser($email)->getName(); } catch (InvalidArgumentException $e) { return null; } } private function findUser(string $email): User { $user = $this->userRepository->findBy(['email' => $email]); if (!$user instanceof User) { throw new InvalidArgumentException('User not found.'); } return $user; } FUNCTIONAL PHP 2. Forget about throwing Exceptions
  • 24. HOW? public function getUserName(string $email): ?string { try { return $this->findUser($email)->getName(); } catch (InvalidArgumentException $e) { return null; } } private function findUser(string $email): User { $user = $this->userRepository->findBy(['email' => $email]); if (!$user instanceof User) { throw new InvalidArgumentException('User not found.'); } return $user; } FUNCTIONAL PHP 2. Forget about throwing Exceptions
  • 25. HOW? public function getUserName(string $email): Option { return $this->findUser($email) ->right(function (User $user): Some { return new Some($user->getName()); }) ->left(function (): None { return new None(); }) ->get(); } private function findUser(string $email): Either { return Option::of($this->userRepository->findBy(['email' => $email])) ->map(function (User $user) { return new Right($user); }) ->getOrElse(function () { return new Left('User not found.'); }); } FUNCTIONAL PHP 2. Forget about throwing Exceptions: Use Either instead
  • 26. EITHER FUNCTIONAL PHP abstract class Either { abstract public function left(Closure $expression): Either; abstract public function right(Closure $expression): Either; abstract public function flatLeft(Closure $expression): Either; abstract public function flatRight(Closure $expression): Either; public function get(): mixed {}; }
 class Right extends Either {} class Left extends Either {}
  • 27. EITHER FUNCTIONAL PHP Best practices: 1. Use Right of expected value, Left for unexpected/error 2. Use left(Closure $expression) / right(Closure $expression) when $expression returns unpacked value
 right(function(): mixed): Either 3. Use flatLeft(Closure $expression) / flatRight(Closure $expression) when $expression returns value packed in either Right or Left
 flatRight(function(): Either): Either 4. flatRight() for chaining, right() on success, left() on error 5. Don’t forget to call get() at end! - only at this point all defined expressions are evaluated
  • 28. HOW? FUNCTIONAL PHP 1. Forget about null checks 2. Forget about throwing Exceptions 3. Forget about variables
  • 29. HOW? public function getUserName(string $email): Option { return $this->findUser($email) ->flatRight(function (User $user) { return new Some($user->getName()); }) ->flatLeft(function () { return new None(); }) ->get(); } private function getUser(string $email): Either { return Option::of($this->userRepository->findBy(['email' => $email])) ->map(function (User $user) { return new Right($user); }) ->getOrElse(function () { return new Left('User not found.'); }); } FUNCTIONAL PHP 3. Forget about variables
 Avoid declaring them (I’ve already done it)
  • 30. HOW? public function getUserName(string $email): Option { return $this->findUser($email) ->flatRight(function (User $user) { return new Some($user->getName()); }) ->flatLeft(function () { return new None(); }) ->get(); } private function getUser(string $email): Either { return Option::of($this->userRepository->findBy(['email' => $email])) ->map(function (User $user) { return new Right($user); }) ->getOrElse(function () { return new Left('User not found.'); }); } FUNCTIONAL PHP 3. Forget about variables: Avoid declaring them (I’ve already done it)
  • 31. HOW? FUNCTIONAL PHP 1. Forget about null checks 2. Forget about throwing Exceptions 3. Forget about variables 4. Forget about loops

  • 32. HOW? $data = [1,2,3,4,5]; $result = []; foreach ($data as $key => $value) { $result[$key] = $value * 2; } $data = array_map(function (int $value): int { return $value * 2; }, $data); $data = array_filter($data, function (int $value): bool { return $value % 2 === 0; }); $data = array_reduce($data, function (int $prev, int $value): int { return $prev + $value; }, 0); return $data; FUNCTIONAL PHP 4. Forget about loops
  • 33. HOW? use PhpSlangCollectionParallelListCollection; return ParallelListCollection::of([1,2,3,4,5]) ->map(function (int $value): int { return $value * 2; }) ->filter(function (int $value): bool { return $value % 2 === 0; }) ->fold(0, function (int $prev, int $value): int { return $prev + $value; }); FUNCTIONAL PHP 4. Forget about loops
 Use Collection instead
  • 34. COLLECTIONS FUNCTIONAL PHP Libraries which provide them: 1. Doctrine collections (incomplete functionality) 2. PhpSlang 3. Immutable.php Collections provide unified API for PHP’s array_* functions and sometimes they are even faster than native PHP.
 
 Methods: 1. filter() (array_filter) 2. map() (array_map) 3. reduce() / fold() (array_reduce) 4. sort() (usort)
  • 35. HOW? FUNCTIONAL PHP 1. Forget about null checks 2. Forget about throwing Exceptions 3. Forget about variables 4. Forget about loops 5. Use async calls wherever you can
  • 36. HOW? use function ClueReactBlockawaitAll; /* use try-catch because ReactPHP's awaitAll uses Exceptions to escape loop and PhpSlang's Future monad is not ready yet */ try { $argumentsArray = awaitAll([ $this->getStatus($valuesArray[‘status']), $this->getIdea($valuesArray['idea']), $this->getUserFromToken(), $valuesArray['reason'] ?? null ], LoopFactory::create()); return new Right($argumentsArray); } catch (NotFoundHttpException $e) { return new Left($e); } FUNCTIONAL PHP 5. Use async calls wherever you can
  • 37. HOW? FUNCTIONAL PHP 1. Forget about null checks 2. Forget about loops 3. Forget about throwing Exceptions 4. Forget about variables 5. Use async calls wherever you can 6. Avoid mutating the State
  • 38. $today = new DateTime(); $yesterday = $today->modify('-1 day'); echo "Yesterday was {$yesterday->format('l')} and today is {$today->format('l')}"; FUNCTIONAL PHP HOW? 6. Avoid mutating the State
  • 39. $today = new DateTime(); $yesterday = $today->modify('-1 day'); echo "Yesterday was {$yesterday->format('l')} and today is {$today->format('l')}"; // prints "Yesterday was Thursday and today is Thursday" FUNCTIONAL PHP HOW? 6. Avoid mutating the State
  • 40. $today = new DateTimeImmutable(); $yesterday = $today->modify('-1 day'); echo "Yesterday was {$yesterday->format('l')} and today is {$today->format(‘l')}"; // prints "Yesterday was Thursday and today is Friday" FUNCTIONAL PHP HOW? 6. Avoid mutating the State Use Immutable Classes
  • 43. HOW IS IT CALLED IN OTHER LANGUAGES? FUNCTIONAL PHP JS (ELM) PHP (PHPSLANG) SCALA (JAVASLANG) Maybe (Just, Nothing) Option (Some, None) Option (Some, None) Either (Right, Left) Either (Right, Left) Either (Right, Left) Promise Promise (Future?) Future async, await await, awaitAll Await.result
  • 46. FURTHER READING FUNCTIONAL PHP ▸ Functional Programming basics ▸ http://maciejpirog.github.io/fishy/ ▸ http://adit.io/posts/2013-04-17-functors,_applicatives,_and_monads_in_pictures.html ▸ https://medium.com/@cscalfani/so-you-want-to-be-a-functional-programmer-part-1-1f15e387e536 ▸ Missing parts of PHP (maybe in 7.2…) ▸ https://wiki.php.net/rfc/generics (https://www.bountysource.com/issues/20553561-add-generics-support) ▸ https://wiki.php.net/rfc/immutability ▸ https://wiki.php.net/rfc/pipe-operator ▸ PhpSlang http://phpslang.io/ ▸ https://php-slang.github.io/php-slang-docs/static/Introduction/Thinking_Functional.html ▸ https://php-slang.github.io/php-slang-docs/static/Usage/Essentials/Option.html ▸ ReactPHP http://reactphp.org/ ▸ https://github.com/clue/php-block-react