SlideShare a Scribd company logo
1 of 46
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

PHP 5.3 Overview
PHP 5.3 OverviewPHP 5.3 Overview
PHP 5.3 Overview
jsmith92
 
Php Reusing Code And Writing Functions
Php Reusing Code And Writing FunctionsPhp Reusing Code And Writing Functions
Php Reusing Code And Writing Functions
mussawir20
 
02 - Second meetup
02 - Second meetup02 - Second meetup
02 - Second meetup
EdiPHP
 
Introducing PHP Latest Updates
Introducing PHP Latest UpdatesIntroducing PHP Latest Updates
Introducing PHP Latest Updates
Iftekhar Eather
 

Similar to Elements of Functional Programming in PHP (20)

PHP 5.3 Overview
PHP 5.3 OverviewPHP 5.3 Overview
PHP 5.3 Overview
 
PHP 5.3 Part 2 - Lambda Functions & Closures
PHP 5.3 Part 2 - Lambda Functions & ClosuresPHP 5.3 Part 2 - Lambda Functions & Closures
PHP 5.3 Part 2 - Lambda Functions & Closures
 
A Functional Guide to Cat Herding with PHP Generators
A Functional Guide to Cat Herding with PHP GeneratorsA Functional Guide to Cat Herding with PHP Generators
A Functional Guide to Cat Herding with PHP Generators
 
Go OO! - Real-life Design Patterns in PHP 5
Go OO! - Real-life Design Patterns in PHP 5Go OO! - Real-life Design Patterns in PHP 5
Go OO! - Real-life Design Patterns in PHP 5
 
08 Advanced PHP #burningkeyboards
08 Advanced PHP #burningkeyboards08 Advanced PHP #burningkeyboards
08 Advanced PHP #burningkeyboards
 
関西PHP勉強会 php5.4つまみぐい
関西PHP勉強会 php5.4つまみぐい関西PHP勉強会 php5.4つまみぐい
関西PHP勉強会 php5.4つまみぐい
 
Functional php
Functional phpFunctional php
Functional php
 
PHP Basics
PHP BasicsPHP Basics
PHP Basics
 
Preparing for the next PHP version (5.6)
Preparing for the next PHP version (5.6)Preparing for the next PHP version (5.6)
Preparing for the next PHP version (5.6)
 
Php Reusing Code And Writing Functions
Php Reusing Code And Writing FunctionsPhp Reusing Code And Writing Functions
Php Reusing Code And Writing Functions
 
PHP7 - Scalar Type Hints & Return Types
PHP7 - Scalar Type Hints & Return TypesPHP7 - Scalar Type Hints & Return Types
PHP7 - Scalar Type Hints & Return Types
 
Slide
SlideSlide
Slide
 
PHP Conference Asia 2016
PHP Conference Asia 2016PHP Conference Asia 2016
PHP Conference Asia 2016
 
Hack tutorial
Hack tutorialHack tutorial
Hack tutorial
 
02 - Second meetup
02 - Second meetup02 - Second meetup
02 - Second meetup
 
Introducing PHP Latest Updates
Introducing PHP Latest UpdatesIntroducing PHP Latest Updates
Introducing PHP Latest Updates
 
Functional Programming for OO Programmers (part 2)
Functional Programming for OO Programmers (part 2)Functional Programming for OO Programmers (part 2)
Functional Programming for OO Programmers (part 2)
 
Wien15 java8
Wien15 java8Wien15 java8
Wien15 java8
 
Functional programming with php7
Functional programming with php7Functional programming with php7
Functional programming with php7
 
Solid principles
Solid principlesSolid principles
Solid principles
 

Recently uploaded

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
negromaestrong
 

Recently uploaded (20)

Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Magic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptxMagic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptx
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 

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