Log In
Or create an account ->
Imperial Library
Home
About
News
Upload
Forum
Help
Login/SignUp
Index
Functional PHP
Functional PHP
Credits
About the Author
About the Reviewer
www.PacktPub.com
Why subscribe?
Customer Feedback
Preface
What this book covers
What you need for this book
Who this book is for
Conventions
Reader feedback
Customer support
Downloading the example code
Errata
Piracy
Questions
1. Functions as First Class Citizens in PHP
Before we begin
Coding standards
Autoloading and Composer
Functions and methods
PHP 7 scalar type hints
Anonymous functions
Closures
Closures inside of classes
Using objects as functions
The Closure class
Higher-order functions
What is a callable?
Summary
2. Pure Functions, Referential Transparency, and Immutability
Two sets of input and output
Pure functions
What about encapsulation?
Spotting side causes
Spotting side effects
What about object methods?
Closing words
Immutability
Why does immutability matter?
Data sharing
Using constants
An RFC is on its way
Value objects
Libraries for immutable collections
Laravel Collection
Immutable.php
Referential transparency
Non-strictness or lazy evaluation
Performance
Code readability
Infinite lists or streams
Code optimization
Memoization
PHP in all that?
Summary
3. Functional Basis in PHP
General advice
Making all inputs explicit
Avoiding temporary variables
Smaller functions
Parameter order matters
The map function
The filter function
The fold or reduce function
The map and filter functions using fold
Folding left and right
The MapReduce model
Convolution or zip
Recursion
Recursion and loops
Exceptions
PHP 7 and exceptions
Alternatives to exceptions
Logging/displaying error message
Error codes
Default value/null
Error handler
The Option/Maybe and Either types
Lifting functions
The Either type
Libraries
The functional-php library
How to use the functions
General helpers
Extending PHP functions
Working with predicates
Invoking functions
Manipulating data
Wrapping up
The php-option library
Laravel collections
Working with Laravel's Collections
The immutable-php library
Using immutable.php
Other libraries
The Underscore.php library
Saber
Rawr
PHP Functional
Functional
PHP functional programming Utils
Non-standard PHP library
Summary
4. Composing Functions
Composing functions
Partial application
Currying
Currying functions in PHP
Parameter order matters a lot!
Using composition to solve real issues
Summary
5. Functors, Applicatives, and Monads
Functors
Identity function
Functor laws
Identity functor
Closing words
Applicative functors
The applicative abstraction
Applicative laws
Map
Identity
Homomorphism
Interchange
Composition
Verifying that the laws hold
Using applicatives
Monoids
Identity law
Associativity law
Verifying that the laws hold
What are monoids useful for?
A monoid implementation
Our first monoids
Using monoids
Monads
Monad laws
Left identity
Right identity
Associativity
Validating our monads
Why monads?
Another take on monads
A quick monad example
Further reading
Summary
6. Real-Life Monads
Monadic helper methods
The filterM method
The foldM method
Closing words
Maybe and Either monads
Motivation
Implementation
Examples
List monad
Motivation
Implementation
Examples
Where can the knight go?
Writer monad
Motivation
Implementation
Examples
Reader monad
Motivation
Implementation
Examples
State monad
Motivation
Implementation
Examples
IO monad
Motivation
Implementation
Examples
Summary
7. Functional Techniques and Topics
Type systems
The Hindley-Milner type system
Type signatures
Free theorems
Closing words
Point-free style
Using const for functions
Recursion, stack overflows, and trampolines
Tail-calls
Tail-call elimination
From recursion to tail recursion
Stack overflows
Trampolines
Multi-step recursion
The trampoline library
Alternative method
Closing words
Pattern matching
Pattern matching in PHP
Better switch statements
Other usages
Type classes
Algebraic structures and category theory
From mathematics to computer science
Important mathematical terms
Fantasy Land
Monad transformers
Lenses
Summary
8. Testing
Testing vocabulary
Testing pure functions
All inputs are explicit
Referential transparency and no side-effects
Simplified mocking
Building blocks
Closing words
Speeding up using parallelization
Property-based testing
What exactly is a property?
Implementing the add function
The PhpQuickCheck testing library
Eris
Closing words
Summary
9. Performance Efficiency
Performance impact
Does the overhead matter?
Let's not forget
Can we do something?
Closing words
Memoization
Haskell, Scala, and memoization
Closing words
Parallelization of computation
Parallel tasks in PHP
The pthreads extension
Messaging queues
Other options
Closing words
Summary
10. PHP Frameworks and FP
Symfony
Handling the request
Database entities
Embeddables
Avoiding setters
Why immutable entities?
Symfony ParamConverter
Maybe there is an entity
Organizing your business logic
Flash messages, sessions, and other APIs with side-effects
Closing words
Laravel
Database results
Using Maybe
Getting rid of facades
HTTP request
Closing words
Drupal
Database access
Dealing with hooks requiring side effects
Hook orders
Closing words
WordPress
Database access
Benefits of a functional approach
Closing words
Summary
11. Designing a Functional Application
Architecture of a purely functional application
From Functional Reactive Animation to Functional Reactive Programming
Reactive programming
Functional Reactive Programming
Time traveling
Disclaimer
Going further
ReactiveX primer
RxPHP
Achieving referential transparency
Summary
12. What Are We Talking about When We Talk about Functional Programming
What is functional programming all about?
Functions
Declarative programming
Avoiding mutable state
Why is functional programming the future of software development?
Reducing the cognitive burden on developers
Keeping the state away
Small building blocks
Locality of concerns
Declarative programming
Software with fewer bugs
Easier refactoring
Parallel execution
Enforcing good practices
A quick history of the functional world
The first years
The Lisp family
ML
The rise of Erlang
Haskell
Scala
The newcomers
Functional jargon
← Prev
Back
Next →
← Prev
Back
Next →