SlideShare a Scribd company logo
Functional Programming in PHP
A secret place behind OOP shadow
– Michael Feathers
“OO makes code understandable by encapsulating moving parts.
FP makes code understandable by minimizing moving parts.”
- Someone from internet
“...The less moving parts a machine has, the less likely it is going
to break down...”
So it means OOP is bad?
Not really...
So what we should do when?
We already use them together
Where??? How??
Does this look familiar?
Does this look familiar?
This is called functional programming
Functional Programming in PHP
• PHP supports first-class functions
• Both user-defined and built-in functions can be referenced by a variable and invoked
dynamically
• Functions can be passed as arguments to other functions and a function can return other
functions (a feature called higher-order functions)
• Recursion, a feature that allows a function to call itself, is supported by the language, but most
of the PHP code focus is on iteration.
• Anonymous functions (with support for closures) have been present since PHP 5.3 (2009).
• PHP 5.4 added the ability to bind closures to an object’s scope and also improved support for
callables such that they can be used interchangeably with anonymous functions in almost all
cases
Functional Programming in PHP
• The most common usage of higher-order functions is when
implementing a strategy pattern. The built-in array_filter() function
asks both for the input array (data) and a function (a strategy or a
callback) used as a filter function on each array item.
• AND... That's pretty much all uses cases in standard development
• But wait!!! It should be more use cases?!
How many times did you find yourself writing the
same generic code over and over?
What do you think about this?
How does it look?
• More simpler?
• More understandable?
• More foolproof?
• Less complex?
• jQuery/Underscore.js for PHP?
When I have some questions
• Why we are using principles like DRY, KISS but don't apply them?
• Why we constantly write test to cover the same generic code?
• Why we write complex code over and over?
Thank you!

More Related Content

What's hot

Ria Applications And PHP
Ria Applications And PHPRia Applications And PHP
Ria Applications And PHP
John Coggeshall
 
Proxying DBI with DBD::Gofer and App::Staticperl
Proxying DBI with DBD::Gofer and App::StaticperlProxying DBI with DBD::Gofer and App::Staticperl
Proxying DBI with DBD::Gofer and App::Staticperl
nohuhu
 
Automating JavaScript testing with Jasmine and Perl
Automating JavaScript testing with Jasmine and PerlAutomating JavaScript testing with Jasmine and Perl
Automating JavaScript testing with Jasmine and Perl
nohuhu
 
Developing Rich Internet Applications with Perl and JavaScript
Developing Rich Internet Applications with Perl and JavaScriptDeveloping Rich Internet Applications with Perl and JavaScript
Developing Rich Internet Applications with Perl and JavaScript
nohuhu
 
SGCE 2015 REST APIs
SGCE 2015 REST APIsSGCE 2015 REST APIs
SGCE 2015 REST APIs
Domingo Suarez Torres
 
Riak at Posterous
Riak at PosterousRiak at Posterous
Riak at Posterous
capotej
 
Sylius ecommerce solution for Symfony2 (WebCamp Ljubljana)
Sylius ecommerce solution for Symfony2 (WebCamp Ljubljana)Sylius ecommerce solution for Symfony2 (WebCamp Ljubljana)
Sylius ecommerce solution for Symfony2 (WebCamp Ljubljana)
Antonio Peric-Mazar
 
Making CLI app in ruby
Making CLI app in rubyMaking CLI app in ruby
Making CLI app in ruby
Huy Do
 
RubyConf Taiwan 2016 - Large scale Rails applications
RubyConf Taiwan 2016 - Large scale Rails applicationsRubyConf Taiwan 2016 - Large scale Rails applications
RubyConf Taiwan 2016 - Large scale Rails applications
Florian Dutey
 
Introduction to Django (GDays 2014)
Introduction to Django (GDays 2014) Introduction to Django (GDays 2014)
Introduction to Django (GDays 2014)
Edem Kumodzi
 
Stackato v6
Stackato v6Stackato v6
Stackato v6
Jonas Brømsø
 
Api crash
Api crashApi crash
Api crash
Hoang Nguyen
 
GitHub as a Landing Page
GitHub as a Landing Page GitHub as a Landing Page
GitHub as a Landing Page
Pronovix
 
[Start] Playing
[Start] Playing[Start] Playing
[Start] Playing
佑介 九岡
 
Automating everything with Microsoft Flow
Automating everything with Microsoft FlowAutomating everything with Microsoft Flow
Automating everything with Microsoft Flow
Jaap Brasser
 
Approaching APIs
Approaching APIsApproaching APIs
Approaching APIs
Ross Singer
 

What's hot (16)

Ria Applications And PHP
Ria Applications And PHPRia Applications And PHP
Ria Applications And PHP
 
Proxying DBI with DBD::Gofer and App::Staticperl
Proxying DBI with DBD::Gofer and App::StaticperlProxying DBI with DBD::Gofer and App::Staticperl
Proxying DBI with DBD::Gofer and App::Staticperl
 
Automating JavaScript testing with Jasmine and Perl
Automating JavaScript testing with Jasmine and PerlAutomating JavaScript testing with Jasmine and Perl
Automating JavaScript testing with Jasmine and Perl
 
Developing Rich Internet Applications with Perl and JavaScript
Developing Rich Internet Applications with Perl and JavaScriptDeveloping Rich Internet Applications with Perl and JavaScript
Developing Rich Internet Applications with Perl and JavaScript
 
SGCE 2015 REST APIs
SGCE 2015 REST APIsSGCE 2015 REST APIs
SGCE 2015 REST APIs
 
Riak at Posterous
Riak at PosterousRiak at Posterous
Riak at Posterous
 
Sylius ecommerce solution for Symfony2 (WebCamp Ljubljana)
Sylius ecommerce solution for Symfony2 (WebCamp Ljubljana)Sylius ecommerce solution for Symfony2 (WebCamp Ljubljana)
Sylius ecommerce solution for Symfony2 (WebCamp Ljubljana)
 
Making CLI app in ruby
Making CLI app in rubyMaking CLI app in ruby
Making CLI app in ruby
 
RubyConf Taiwan 2016 - Large scale Rails applications
RubyConf Taiwan 2016 - Large scale Rails applicationsRubyConf Taiwan 2016 - Large scale Rails applications
RubyConf Taiwan 2016 - Large scale Rails applications
 
Introduction to Django (GDays 2014)
Introduction to Django (GDays 2014) Introduction to Django (GDays 2014)
Introduction to Django (GDays 2014)
 
Stackato v6
Stackato v6Stackato v6
Stackato v6
 
Api crash
Api crashApi crash
Api crash
 
GitHub as a Landing Page
GitHub as a Landing Page GitHub as a Landing Page
GitHub as a Landing Page
 
[Start] Playing
[Start] Playing[Start] Playing
[Start] Playing
 
Automating everything with Microsoft Flow
Automating everything with Microsoft FlowAutomating everything with Microsoft Flow
Automating everything with Microsoft Flow
 
Approaching APIs
Approaching APIsApproaching APIs
Approaching APIs
 

Similar to Functional Programming in PHP

Introduction to Functional Programming
Introduction to Functional ProgrammingIntroduction to Functional Programming
Introduction to Functional Programming
Dave Fancher
 
Chapter1_ObjectOrientedProgramming.pptx
Chapter1_ObjectOrientedProgramming.pptxChapter1_ObjectOrientedProgramming.pptx
Chapter1_ObjectOrientedProgramming.pptx
pamyasstos
 
Oop's
Oop'sOop's
OOPs-Interview-Questions.pdf
OOPs-Interview-Questions.pdfOOPs-Interview-Questions.pdf
OOPs-Interview-Questions.pdf
Samir Paul
 
PHP - Introduction to PHP Fundamentals
PHP -  Introduction to PHP FundamentalsPHP -  Introduction to PHP Fundamentals
PHP - Introduction to PHP Fundamentals
Vibrant Technologies & Computers
 
Learn PHP Lacture1
Learn PHP Lacture1Learn PHP Lacture1
Learn PHP Lacture1
ADARSH BHATT
 
Why functional programming in C# & F#
Why functional programming in C# & F#Why functional programming in C# & F#
Why functional programming in C# & F#
Riccardo Terrell
 
Oop.pptx
Oop.pptxOop.pptx
Oop.pptx
KalGetachew2
 
Exploring PHP's Built-in Functions
Exploring PHP's Built-in FunctionsExploring PHP's Built-in Functions
Exploring PHP's Built-in Functions
Emma Thompson
 
Php
PhpPhp
Php
PhpPhp
Php
PhpPhp
Object Oriented programming - Introduction
Object Oriented programming - IntroductionObject Oriented programming - Introduction
Object Oriented programming - Introduction
Madishetty Prathibha
 
Php internal architecture
Php internal architecturePhp internal architecture
Php internal architecture
Elizabeth Smith
 
Top 100 PHP Questions and Answers
Top 100 PHP Questions and AnswersTop 100 PHP Questions and Answers
Top 100 PHP Questions and Answers
iimjobs and hirist
 
Oops in PHP By Nyros Developer
Oops in PHP By Nyros DeveloperOops in PHP By Nyros Developer
Oops in PHP By Nyros Developer
Nyros Technologies
 
php basic part one
php basic part onephp basic part one
php basic part one
jeweltutin
 
PHP Internals
PHP InternalsPHP Internals
PHP Internals
Nico Loubser
 
Beginning PHP
Beginning PHPBeginning PHP
Beginning PHP
Mohammed Safwat
 
Introduction to php
Introduction to phpIntroduction to php
Introduction to php
Sush Singhaniya
 

Similar to Functional Programming in PHP (20)

Introduction to Functional Programming
Introduction to Functional ProgrammingIntroduction to Functional Programming
Introduction to Functional Programming
 
Chapter1_ObjectOrientedProgramming.pptx
Chapter1_ObjectOrientedProgramming.pptxChapter1_ObjectOrientedProgramming.pptx
Chapter1_ObjectOrientedProgramming.pptx
 
Oop's
Oop'sOop's
Oop's
 
OOPs-Interview-Questions.pdf
OOPs-Interview-Questions.pdfOOPs-Interview-Questions.pdf
OOPs-Interview-Questions.pdf
 
PHP - Introduction to PHP Fundamentals
PHP -  Introduction to PHP FundamentalsPHP -  Introduction to PHP Fundamentals
PHP - Introduction to PHP Fundamentals
 
Learn PHP Lacture1
Learn PHP Lacture1Learn PHP Lacture1
Learn PHP Lacture1
 
Why functional programming in C# & F#
Why functional programming in C# & F#Why functional programming in C# & F#
Why functional programming in C# & F#
 
Oop.pptx
Oop.pptxOop.pptx
Oop.pptx
 
Exploring PHP's Built-in Functions
Exploring PHP's Built-in FunctionsExploring PHP's Built-in Functions
Exploring PHP's Built-in Functions
 
Php
PhpPhp
Php
 
Php
PhpPhp
Php
 
Php
PhpPhp
Php
 
Object Oriented programming - Introduction
Object Oriented programming - IntroductionObject Oriented programming - Introduction
Object Oriented programming - Introduction
 
Php internal architecture
Php internal architecturePhp internal architecture
Php internal architecture
 
Top 100 PHP Questions and Answers
Top 100 PHP Questions and AnswersTop 100 PHP Questions and Answers
Top 100 PHP Questions and Answers
 
Oops in PHP By Nyros Developer
Oops in PHP By Nyros DeveloperOops in PHP By Nyros Developer
Oops in PHP By Nyros Developer
 
php basic part one
php basic part onephp basic part one
php basic part one
 
PHP Internals
PHP InternalsPHP Internals
PHP Internals
 
Beginning PHP
Beginning PHPBeginning PHP
Beginning PHP
 
Introduction to php
Introduction to phpIntroduction to php
Introduction to php
 

Recently uploaded

From Natural Language to Structured Solr Queries using LLMs
From Natural Language to Structured Solr Queries using LLMsFrom Natural Language to Structured Solr Queries using LLMs
From Natural Language to Structured Solr Queries using LLMs
Sease
 
"Frontline Battles with DDoS: Best practices and Lessons Learned", Igor Ivaniuk
"Frontline Battles with DDoS: Best practices and Lessons Learned",  Igor Ivaniuk"Frontline Battles with DDoS: Best practices and Lessons Learned",  Igor Ivaniuk
"Frontline Battles with DDoS: Best practices and Lessons Learned", Igor Ivaniuk
Fwdays
 
Poznań ACE event - 19.06.2024 Team 24 Wrapup slidedeck
Poznań ACE event - 19.06.2024 Team 24 Wrapup slidedeckPoznań ACE event - 19.06.2024 Team 24 Wrapup slidedeck
Poznań ACE event - 19.06.2024 Team 24 Wrapup slidedeck
FilipTomaszewski5
 
Leveraging the Graph for Clinical Trials and Standards
Leveraging the Graph for Clinical Trials and StandardsLeveraging the Graph for Clinical Trials and Standards
Leveraging the Graph for Clinical Trials and Standards
Neo4j
 
AppSec PNW: Android and iOS Application Security with MobSF
AppSec PNW: Android and iOS Application Security with MobSFAppSec PNW: Android and iOS Application Security with MobSF
AppSec PNW: Android and iOS Application Security with MobSF
Ajin Abraham
 
"Choosing proper type of scaling", Olena Syrota
"Choosing proper type of scaling", Olena Syrota"Choosing proper type of scaling", Olena Syrota
"Choosing proper type of scaling", Olena Syrota
Fwdays
 
Principle of conventional tomography-Bibash Shahi ppt..pptx
Principle of conventional tomography-Bibash Shahi ppt..pptxPrinciple of conventional tomography-Bibash Shahi ppt..pptx
Principle of conventional tomography-Bibash Shahi ppt..pptx
BibashShahi
 
Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)
Jakub Marek
 
QA or the Highway - Component Testing: Bridging the gap between frontend appl...
QA or the Highway - Component Testing: Bridging the gap between frontend appl...QA or the Highway - Component Testing: Bridging the gap between frontend appl...
QA or the Highway - Component Testing: Bridging the gap between frontend appl...
zjhamm304
 
Demystifying Knowledge Management through Storytelling
Demystifying Knowledge Management through StorytellingDemystifying Knowledge Management through Storytelling
Demystifying Knowledge Management through Storytelling
Enterprise Knowledge
 
"NATO Hackathon Winner: AI-Powered Drug Search", Taras Kloba
"NATO Hackathon Winner: AI-Powered Drug Search",  Taras Kloba"NATO Hackathon Winner: AI-Powered Drug Search",  Taras Kloba
"NATO Hackathon Winner: AI-Powered Drug Search", Taras Kloba
Fwdays
 
Christine's Supplier Sourcing Presentaion.pptx
Christine's Supplier Sourcing Presentaion.pptxChristine's Supplier Sourcing Presentaion.pptx
Christine's Supplier Sourcing Presentaion.pptx
christinelarrosa
 
What is an RPA CoE? Session 1 – CoE Vision
What is an RPA CoE?  Session 1 – CoE VisionWhat is an RPA CoE?  Session 1 – CoE Vision
What is an RPA CoE? Session 1 – CoE Vision
DianaGray10
 
Apps Break Data
Apps Break DataApps Break Data
Apps Break Data
Ivo Velitchkov
 
"What does it really mean for your system to be available, or how to define w...
"What does it really mean for your system to be available, or how to define w..."What does it really mean for your system to be available, or how to define w...
"What does it really mean for your system to be available, or how to define w...
Fwdays
 
Freshworks Rethinks NoSQL for Rapid Scaling & Cost-Efficiency
Freshworks Rethinks NoSQL for Rapid Scaling & Cost-EfficiencyFreshworks Rethinks NoSQL for Rapid Scaling & Cost-Efficiency
Freshworks Rethinks NoSQL for Rapid Scaling & Cost-Efficiency
ScyllaDB
 
GNSS spoofing via SDR (Criptored Talks 2024)
GNSS spoofing via SDR (Criptored Talks 2024)GNSS spoofing via SDR (Criptored Talks 2024)
GNSS spoofing via SDR (Criptored Talks 2024)
Javier Junquera
 
Christine's Product Research Presentation.pptx
Christine's Product Research Presentation.pptxChristine's Product Research Presentation.pptx
Christine's Product Research Presentation.pptx
christinelarrosa
 
Introduction of Cybersecurity with OSS at Code Europe 2024
Introduction of Cybersecurity with OSS  at Code Europe 2024Introduction of Cybersecurity with OSS  at Code Europe 2024
Introduction of Cybersecurity with OSS at Code Europe 2024
Hiroshi SHIBATA
 
Day 2 - Intro to UiPath Studio Fundamentals
Day 2 - Intro to UiPath Studio FundamentalsDay 2 - Intro to UiPath Studio Fundamentals
Day 2 - Intro to UiPath Studio Fundamentals
UiPathCommunity
 

Recently uploaded (20)

From Natural Language to Structured Solr Queries using LLMs
From Natural Language to Structured Solr Queries using LLMsFrom Natural Language to Structured Solr Queries using LLMs
From Natural Language to Structured Solr Queries using LLMs
 
"Frontline Battles with DDoS: Best practices and Lessons Learned", Igor Ivaniuk
"Frontline Battles with DDoS: Best practices and Lessons Learned",  Igor Ivaniuk"Frontline Battles with DDoS: Best practices and Lessons Learned",  Igor Ivaniuk
"Frontline Battles with DDoS: Best practices and Lessons Learned", Igor Ivaniuk
 
Poznań ACE event - 19.06.2024 Team 24 Wrapup slidedeck
Poznań ACE event - 19.06.2024 Team 24 Wrapup slidedeckPoznań ACE event - 19.06.2024 Team 24 Wrapup slidedeck
Poznań ACE event - 19.06.2024 Team 24 Wrapup slidedeck
 
Leveraging the Graph for Clinical Trials and Standards
Leveraging the Graph for Clinical Trials and StandardsLeveraging the Graph for Clinical Trials and Standards
Leveraging the Graph for Clinical Trials and Standards
 
AppSec PNW: Android and iOS Application Security with MobSF
AppSec PNW: Android and iOS Application Security with MobSFAppSec PNW: Android and iOS Application Security with MobSF
AppSec PNW: Android and iOS Application Security with MobSF
 
"Choosing proper type of scaling", Olena Syrota
"Choosing proper type of scaling", Olena Syrota"Choosing proper type of scaling", Olena Syrota
"Choosing proper type of scaling", Olena Syrota
 
Principle of conventional tomography-Bibash Shahi ppt..pptx
Principle of conventional tomography-Bibash Shahi ppt..pptxPrinciple of conventional tomography-Bibash Shahi ppt..pptx
Principle of conventional tomography-Bibash Shahi ppt..pptx
 
Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)
 
QA or the Highway - Component Testing: Bridging the gap between frontend appl...
QA or the Highway - Component Testing: Bridging the gap between frontend appl...QA or the Highway - Component Testing: Bridging the gap between frontend appl...
QA or the Highway - Component Testing: Bridging the gap between frontend appl...
 
Demystifying Knowledge Management through Storytelling
Demystifying Knowledge Management through StorytellingDemystifying Knowledge Management through Storytelling
Demystifying Knowledge Management through Storytelling
 
"NATO Hackathon Winner: AI-Powered Drug Search", Taras Kloba
"NATO Hackathon Winner: AI-Powered Drug Search",  Taras Kloba"NATO Hackathon Winner: AI-Powered Drug Search",  Taras Kloba
"NATO Hackathon Winner: AI-Powered Drug Search", Taras Kloba
 
Christine's Supplier Sourcing Presentaion.pptx
Christine's Supplier Sourcing Presentaion.pptxChristine's Supplier Sourcing Presentaion.pptx
Christine's Supplier Sourcing Presentaion.pptx
 
What is an RPA CoE? Session 1 – CoE Vision
What is an RPA CoE?  Session 1 – CoE VisionWhat is an RPA CoE?  Session 1 – CoE Vision
What is an RPA CoE? Session 1 – CoE Vision
 
Apps Break Data
Apps Break DataApps Break Data
Apps Break Data
 
"What does it really mean for your system to be available, or how to define w...
"What does it really mean for your system to be available, or how to define w..."What does it really mean for your system to be available, or how to define w...
"What does it really mean for your system to be available, or how to define w...
 
Freshworks Rethinks NoSQL for Rapid Scaling & Cost-Efficiency
Freshworks Rethinks NoSQL for Rapid Scaling & Cost-EfficiencyFreshworks Rethinks NoSQL for Rapid Scaling & Cost-Efficiency
Freshworks Rethinks NoSQL for Rapid Scaling & Cost-Efficiency
 
GNSS spoofing via SDR (Criptored Talks 2024)
GNSS spoofing via SDR (Criptored Talks 2024)GNSS spoofing via SDR (Criptored Talks 2024)
GNSS spoofing via SDR (Criptored Talks 2024)
 
Christine's Product Research Presentation.pptx
Christine's Product Research Presentation.pptxChristine's Product Research Presentation.pptx
Christine's Product Research Presentation.pptx
 
Introduction of Cybersecurity with OSS at Code Europe 2024
Introduction of Cybersecurity with OSS  at Code Europe 2024Introduction of Cybersecurity with OSS  at Code Europe 2024
Introduction of Cybersecurity with OSS at Code Europe 2024
 
Day 2 - Intro to UiPath Studio Fundamentals
Day 2 - Intro to UiPath Studio FundamentalsDay 2 - Intro to UiPath Studio Fundamentals
Day 2 - Intro to UiPath Studio Fundamentals
 

Functional Programming in PHP

  • 1. Functional Programming in PHP A secret place behind OOP shadow
  • 2. – Michael Feathers “OO makes code understandable by encapsulating moving parts. FP makes code understandable by minimizing moving parts.”
  • 3. - Someone from internet “...The less moving parts a machine has, the less likely it is going to break down...”
  • 4. So it means OOP is bad?
  • 5. Not really... So what we should do when?
  • 6. We already use them together Where??? How??
  • 7. Does this look familiar?
  • 8. Does this look familiar? This is called functional programming
  • 9. Functional Programming in PHP • PHP supports first-class functions • Both user-defined and built-in functions can be referenced by a variable and invoked dynamically • Functions can be passed as arguments to other functions and a function can return other functions (a feature called higher-order functions) • Recursion, a feature that allows a function to call itself, is supported by the language, but most of the PHP code focus is on iteration. • Anonymous functions (with support for closures) have been present since PHP 5.3 (2009). • PHP 5.4 added the ability to bind closures to an object’s scope and also improved support for callables such that they can be used interchangeably with anonymous functions in almost all cases
  • 10. Functional Programming in PHP • The most common usage of higher-order functions is when implementing a strategy pattern. The built-in array_filter() function asks both for the input array (data) and a function (a strategy or a callback) used as a filter function on each array item. • AND... That's pretty much all uses cases in standard development • But wait!!! It should be more use cases?!
  • 11. How many times did you find yourself writing the same generic code over and over?
  • 12. What do you think about this?
  • 13. How does it look? • More simpler? • More understandable? • More foolproof? • Less complex? • jQuery/Underscore.js for PHP?
  • 14. When I have some questions • Why we are using principles like DRY, KISS but don't apply them? • Why we constantly write test to cover the same generic code? • Why we write complex code over and over?