KEMBAR78
Functional Programming in PHP | PPTX
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!

Functional Programming in PHP

  • 1.
    Functional Programming inPHP A secret place behind OOP shadow
  • 2.
    – Michael Feathers “OOmakes code understandable by encapsulating moving parts. FP makes code understandable by minimizing moving parts.”
  • 3.
    - Someone frominternet “...The less moving parts a machine has, the less likely it is going to break down...”
  • 4.
    So it meansOOP is bad?
  • 5.
    Not really... So whatwe should do when?
  • 6.
    We already usethem together Where??? How??
  • 7.
    Does this lookfamiliar?
  • 8.
    Does this lookfamiliar? This is called functional programming
  • 9.
    Functional Programming inPHP • 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 inPHP • 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 timesdid you find yourself writing the same generic code over and over?
  • 12.
    What do youthink about this?
  • 13.
    How does itlook? • More simpler? • More understandable? • More foolproof? • Less complex? • jQuery/Underscore.js for PHP?
  • 14.
    When I havesome 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?
  • 15.