100% found this document useful (1 vote)
491 views

Download Full Pro Functional PHP Programming Application Development Strategies for Performance Optimization, Concurrency, Testability, and Code Brevity Aley PDF All Chapters

Brevity

Uploaded by

suquenreitel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
491 views

Download Full Pro Functional PHP Programming Application Development Strategies for Performance Optimization, Concurrency, Testability, and Code Brevity Aley PDF All Chapters

Brevity

Uploaded by

suquenreitel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 52

Download the full version of the textbook now at textbookfull.

com

Pro Functional PHP Programming Application


Development Strategies for Performance
Optimization, Concurrency, Testability, and
Code Brevity Aley
https://textbookfull.com/product/pro-functional-
php-programming-application-development-
strategies-for-performance-optimization-
concurrency-testability-and-code-brevity-aley/

Explore and download more textbook at https://textbookfull.com


Recommended digital products (PDF, EPUB, MOBI) that
you can download immediately if you are interested.

Pro Functional PHP Programming: Application Development


Strategies for Performance Optimization, Concurrency,
Testability, and Code Brevity Rob Aley
https://textbookfull.com/product/pro-functional-php-programming-
application-development-strategies-for-performance-optimization-
concurrency-testability-and-code-brevity-rob-aley/
textbookfull.com

Biota Grow 2C gather 2C cook Loucas

https://textbookfull.com/product/biota-grow-2c-gather-2c-cook-loucas/

textbookfull.com

Pro TypeScript: application-scale javascript development


Second Edition Fenton

https://textbookfull.com/product/pro-typescript-application-scale-
javascript-development-second-edition-fenton/

textbookfull.com

Gone to Pitchipoi A Boy s Desperate Fight for Survival in


Wartime Jews of Poland 1st Edition Katz Rubin

https://textbookfull.com/product/gone-to-pitchipoi-a-boy-s-desperate-
fight-for-survival-in-wartime-jews-of-poland-1st-edition-katz-rubin/

textbookfull.com
Think Like a Warrior The Five Inner Beliefs That Make You
Unstoppable 1st Edition Darrin Donnelly

https://textbookfull.com/product/think-like-a-warrior-the-five-inner-
beliefs-that-make-you-unstoppable-1st-edition-darrin-donnelly/

textbookfull.com

Managing Sport Development 2nd Edition Emma Sherry

https://textbookfull.com/product/managing-sport-development-2nd-
edition-emma-sherry/

textbookfull.com

How to Own the World A Plain English Guide to Thinking


Globally and Investing Wisely The new edition of the life
changing personal finance bestseller Andrew Craig [Craig
https://textbookfull.com/product/how-to-own-the-world-a-plain-english-
guide-to-thinking-globally-and-investing-wisely-the-new-edition-of-
the-life-changing-personal-finance-bestseller-andrew-craig-craig/
textbookfull.com

IoT: Security and Privacy Paradigm (Internet of Everything


(IoE)) 1st Edition Souvik Pal (Editor)

https://textbookfull.com/product/iot-security-and-privacy-paradigm-
internet-of-everything-ioe-1st-edition-souvik-pal-editor/

textbookfull.com

Machine Learning Design Patterns Solutions to Common


Challenges in Data Preparation Model Building and MLOps
1st Edition Valliappa Lakshmanan Sara Robinson Michael
Munn
https://textbookfull.com/product/machine-learning-design-patterns-
solutions-to-common-challenges-in-data-preparation-model-building-and-
mlops-1st-edition-valliappa-lakshmanan-sara-robinson-michael-munn/
textbookfull.com
Corporate Innovation in the Fifth Era Lessons from
Alphabet Google Amazon Apple Facebook and Microsoft
Matthew Le Merle
https://textbookfull.com/product/corporate-innovation-in-the-fifth-
era-lessons-from-alphabet-google-amazon-apple-facebook-and-microsoft-
matthew-le-merle/
textbookfull.com
Rob Aley

Pro Functional PHP Programming


Application Development Strategies for
Performance Optimization, Concurrency,
Testability, and Code Brevity
Rob Aley
Oxford, UK

Any source code or other supplementary material referenced by the


author in this book is available to readers on GitHub via the book's
product page, located at www.apress.com/9781484229576 . For
more detailed information, please visit www.apress.com/source-
code .

ISBN 978-1-4842-2957-6 e-ISBN 978-1-4842-2958-3


https://doi.org/10.1007/978-1-4842-2958-3

Library of Congress Control Number: 2017954985

© Rob Aley 2017

This work is subject to copyright. All rights are reserved by the


Publisher, whether the whole or part of the material is concerned,
specifically the rights of translation, reprinting, reuse of illustrations,
recitation, broadcasting, reproduction on microfilms or in any other
physical way, and transmission or information storage and retrieval,
electronic adaptation, computer software, or by similar or dissimilar
methodology now known or hereafter developed.

Trademarked names, logos, and images may appear in this book.


Rather than use a trademark symbol with every occurrence of a
trademarked name, logo, or image we use the names, logos, and
images only in an editorial fashion and to the benefit of the
trademark owner, with no intention of infringement of the
trademark. The use in this publication of trade names, trademarks,
service marks, and similar terms, even if they are not identified as
such, is not to be taken as an expression of opinion as to whether or
not they are subject to proprietary rights.

While the advice and information in this book are believed to be true
and accurate at the date of publication, neither the authors nor the
editors nor the publisher can accept any legal responsibility for any
errors or omissions that may be made. The publisher makes no
warranty, express or implied, with respect to the material contained
herein.

Printed on acid-free paper

Distributed to the book trade worldwide by Springer


Science+Business Media New York, 233 Spring Street, 6th Floor,
New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505,
e-mail orders-ny@springer-sbm.com, or visit
www.springeronline.com. Apress Media, LLC is a California LLC and
the sole member (owner) is Springer Science + Business Media
Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware
corporation.
Acknowledgments
Isaac Newton said, “If I have seen further, it is by standing on the
shoulders of giants.” This book builds on, and I hope adds to, the
work of many others, the most notable of whom I would like to
acknowledge here.
The authors of, and contributors to, the official PHP Manual :
This is an invaluable reference for PHP functions and syntax, to
which I referred frequently during writing this book, both for
fact checking and as an aide-mémoir. Thanks!
The collective PHP and functional programming wisdom of the
Internet : For more than 17 years I’ve used you for learning,
research, play, and profit. There are too many sites and too
many people to list here; if you’ve written about PHP on the
Web, then you may well be one of them. Thanks!
My family : Thanks for allowing me a modicum of time to write
this book and supporting me unconditionally in everything I do.
Usually. If I ask first. And there’s not something more important
going on. And usually with conditions. Thanks!
Contents
Part I: Functional Programming in PHP 7

Chapter 1:​Introduction

Who Is This Book For?​

What Is Functional Programming?​

Functional Programming Is SOLID

What Are the Benefits of Functional Programming?​

Who Uses Functional Programming, and Why?​

Is Functional Programming “All or Nothing”?​

Further Reading

Why Use PHP for Functional Programming?​

Why Not to Use PHP for Functional Programming

PHP Versions

Conclusion

Chapter 2:​Functional Programming:​Key Concepts

Examining State

Mutability and Immutability

Further Reading

What Is a Function?​
Named Functions

Variable Functions

Language Constructs

Return Values

Lambda/​Anonymous Functions

Higher-Order Functions

Scope

Further Reading

State

Parameters/​Arguments/​Operands, Arity, and Variadic


Functions

Further Reading

Closures

Side Effects

Referential Transparency

Pure Functions

Lists and Collections

Further Reading

Conclusion

Chapter 3:​Getting Started with Functional Patterns


Map, Filter, and Reduce

Recursive Functions

Basic Recursion

Implementing a Recursive Function

Partial Functions

Functional Expressions

Functional Composition

Conclusion

Chapter 4:​Advanced Functional Techniques

Currying Functions

The Mysterious Monad

What Is a Monad?​

The Maybe Monad

Monad Axioms

Monad Axiom 1

Monad Axiom 2

Monad Axiom 3

Testing the Monad Axioms

Other Useful Monads

The IO Monad
Learn More About Monads

Further Reading

Recursion with Trampolines

Recursive Lambdas

The PHP Type System

Type Declarations

Further Reading

Summary

Part II: Application Development Strategies

Chapter 5:​Strategies for High-Performance Applications

Understanding and Measuring Performance

Measuring Performance:​Profiling

Manual Profiling

Profiling Tools

Further Reading and Tools

Low-Level Profiling

Further Reading

Memoization

Further Reading

The Downsides of Memoization


Lazy Evaluation

Further Reading

Generators

Further Reading

The Downsides of Lazy Evaluation

Parallel Programming

Multithreaded Programming

Further Reading

The Standard PHP Library (SPL)

Further Reading

Conclusion

Chapter 6:​Managing Business Logic with Functions

Managing Business Logic

Event-Based Programming

Further Reading

Asynchronous PHP

Further Reading

Chapter 7:​Using Functional Programming in Objected-


Oriented and Procedural Applications

History of PHP Paradigms


Visit https://textbookfull.com
now to explore a rich
collection of eBooks, textbook
and enjoy exciting offers!
Further Reading

PHP Is Not a Functional Language

Objects and Mutability

Further Reading

Immutable Data with Objects

Object Properties As External State

Inline Impurities

Procedural Programming Considerations

Summary

Chapter 8:​Using Helper Libraries in Your Application

How to Choose a Library

Pick Libraries Apart

Libraries Based on Ramda

Pramda

Phamda

Libraries Based on Underscore

Underscore.​php (1)

Underscore

Underscore.​php (2)

Miscellaneous Libraries
Saber

Functional PHP

Other Libraries

Chapter 9:​Processing Big Data with Functional PHP

What Is Big Data?​

Introducing Hadoop

About MapReduce

Installing Hadoop

Tools

Creating Hadoop Jobs in PHP

Further Reading

Chapter 10:​Afterword

Where to Now?​

Giving Feedback and Getting Help and Support

Appendix A: Installing PHP and Libraries

Compiling and Installing PHP

Microsoft Windows

macOS/OS X

Linux/Unix

Compiling and Installing (Extra) Core Extensions


Installing Multiple Versions of PHP

Further Reading

Tools

PEAR and PECL

Composer

Symfony2 Bundles

Getting Help

The PHP Manual

Official Mailing Lists

Stack Overflow

Other Books

Newsgroups

PHP Subredit

PHP on GitHub

File and Data Format Libraries for PHP

Office Documents

Compression, Archiving, and Encryption

Graphics

Audio

Multimedia and Video


Programming, Technical, and Data Interchange

Miscellaneous

Appendix B: Command-Line PHP

PHP Without a Web Server

What’s Different About the CLI SAPI?

Further Reading

CLI SAPI Installation

PHP Command-Line Options

Further Reading

Command-Line Arguments for Your Script

Different Ways to Call PHP Scripts

From a File

From a String

From STDIN

As a Self-Executing Script: Unix/Linux

Further Reading

As a Self-Executing Script: Windows

Windows php-win.exe

“Click to Run” Your PHP

Clickable Icons: Linux


Further Reading

Clickable Icons: Windows

Clickable Icons: Ubuntu Unity

Further Reading

Quitting Your Script from Within

Further Reading

Thinking About Security

Further Reading

CLI-Specific Code Frameworks

Further Reading

PHP REPLs

PsySH

Boris

phpa

PHP Interactive

Sublime-worksheet

phpsh

iPHP

Appendix C: Functional Programming Resources

Other Programming Languages


Functional Programming and Other Paradigms

Articles

Online Books

Videos

Online Courses

Functional Programming Design Patterns

PHP Functional Basics

Data Structures

Mutability in PHP

Map, Filter, Reduce and Other Array Functions

Recursion and Trampolines

Partial Functions and Currying

Functional Composition

Monads

Types

Profiling

Memoization

Lazy Evaluation

Relevant PHP Manual Sections

Parallel Programming
Testing

Event-Based Programming

Asynchronous PHP

Big Data/Hadoop

General-Purpose Libraries

Functional Framework

Lisp in PHP

Other Miscellaneous Topics

PHP RFCs: The Future

The Wikipedia Glossary

Index
Contents at a Glance
About the Author

About the Technical Reviewer

Acknowledgments

Part I: Functional Programming in PHP 7

Chapter 1:​Introduction

Chapter 2:​Functional Programming:​Key Concepts

Chapter 3:​Getting Started with Functional Patterns

Chapter 4:​Advanced Functional Techniques

Part II: Application Development Strategies

Chapter 5:​Strategies for High-Performance Applications

Chapter 6:​Managing Business Logic with Functions


Chapter 7:​Using Functional Programming in Objected-Oriented and
Procedural Applications

Chapter 8:​Using Helper Libraries in Your Application

Chapter 9:​Processing Big Data with Functional PHP

Chapter 10:​Afterword

Appendix A: Installing PHP and Libraries

Appendix B: Command-Line PHP

Appendix C: Functional Programming Resources

Index
About the Author and About the
Technical Reviewer
About the Author
Rob Aley
I’ve been programming in PHP since late
2000. Initially it wasn’t by choice because my
preferred languages at the time were Perl
and Delphi (also known as Object Pascal).
Things began to change after I graduated
from the University of Leeds with a degree in
computer science in 1999 and started out in
a career as a freelance web developer. After
only a couple of months I was offered the
opportunity to take over a (relatively
speaking) substantial government web site
contract from a friend who was exiting the
freelance world for the safer and saner world of full-time
employment. The only catch was that several thousand lines of code
had already been written, and they were written in a relatively new
language called PHP. Oh, and the only other catch was that I had
about a week to learn it before taking over the site. So, as was the
way at the time, I popped down to the local Waterstones bookshop.
(For the younger among you that’s where we used to get books. And
we had to go out and get them. Or order online and wait many days
for them to be delivered.) With my paper copies of The Generic
Beginner’s Complete Guide to PHP and MySQL for Dummies
Compendium (I may not have recalled the titles completely
correctly), I settled down with a pint of ale (I’m in Yorkshire at this
point, remember) and set about reading them. A few days later I
was coding like a pro (well, stuff was working), and 17 years later I
haven’t looked back. Over those 17 years PHP has changed vastly
Visit https://textbookfull.com
now to explore a rich
collection of eBooks, textbook
and enjoy exciting offers!
(the source code for the government web site I mentioned was
littered with comments like “# Would have used a foreach here, if
PHP had one…”) and so have I. I like to think that both I and PHP
have only improved and matured over the years.
After a varied career as a freelancer and starting up a couple of,
er, startups (IT related and not) with varying (usually dismal)
success, I spent the past ten years as a programmer at the
University of Oxford. My day job involved performing medium-scale
data acquisition and management, doing statistical analysis, and
providing user interfaces for researchers and the public. The
majority of my development work was done in PHP, either
developing new projects or gluing together other people’s software,
systems, and databases. I’ve recently left the university to
concentrate on writing books like this and providing consulting and
training (in PHP, information governance, and related areas). But I’m
still programming in PHP!
Throughout my career I’ve always used PHP for web
development, but for desktop GUI work I initially used Delphi (and
then Free-Pascal/Lazarus), complemented with Bash shell scripting
for CLI-based tasks. This was mainly because I learned them while
at university. However, as PHP has matured, I’ve increasingly used it
beyond the Web, and now I rarely use anything else for any
programming or scripting task I encounter. Having been immersed in
other languages such as C++, JavaScript, Fortran, and Lisp (and
probably others that my brain has chosen deliberately not to
remember) by necessity during university and in some of my
freelance jobs, I can honestly say that PHP is now my language of
choice, rather than of necessity. At university (in the late 1990s) I
took a couple of classes that involved functional programming, but
at the time I really didn’t “get the point.” It’s only in recent years
that I’ve picked up functional-style programming again, partly
because of the “buzz” that’s developed around it and partly because
as my programming styles have “matured,” I’ve seen the advantages
to functional coding.
When I’m not tied to a computer, I would like to say I have lots
of varied and interesting hobbies. I used to have. I could write a
whole book (which wouldn’t sell well) about where I’ve been and
what I’ve done, and I’d like to think it’s made me a well-rounded
person. But these days I don’t have any. In large part, this is
because of the demands of my three gorgeous young daughters,
Ellie, Izzy, and Indy; my gorgeous wife, Parv; and my even more
gorgeous cat, Mia. And I wouldn’t have it any other way. That’s what
I tell myself, anyway….
—Rob Aley

About the Technical Reviewer


Christopher Pitt
is a developer and writer, working at SilverStripe. He usually works
on application architecture, though sometimes you’ll find him
building compilers or robots. He is also the author of several web
development books and is a contributor on various open source
projects like AdonisJs.
Part I
Functional Programming in PHP 7
© Rob Aley 2017
Rob Aley, Pro Functional PHP Programming, https://doi.org/10.1007/978-1-4842-
2958-3_1

1. Introduction
Rob Aley1
(1) Oxford, UK

Functional programming isn’t something that is often associated with


PHP. Yet for quite a while PHP has had all the features necessary to
create software using the functional paradigm. In this book, you’ll
take a look at what functional programming is, how to do it in PHP,
and the different ways in which you can use it to improve your PHP
software.
Who Is This Book For?
This book isn’t an introduction to PHP itself; it assumes you have
some basic (or, indeed, advanced) experience in PHP scripting. You
don’t need to be an expert to follow along; I’ll cover all the key
concepts in PHP you’ll need to know to be able to implement
functional designs in your code and point you in the direction of
resources such as web sites and other books that you can use to
learn or investigate any related concepts that I don’t cover directly.
Absolute PHP beginners aside, this book is suitable for all
programmers. Whether you have a pressing need to learn functional
programming (perhaps you’ve taken over a functional PHP code
base) or you are just interested in finding out what the “buzz”
around functional programming is all about, there is something in
this book for you. There’s even likely to be something for those
skeptical about creating software using the functional programming
paradigm. I think that most programmers will find useful lessons and
code patterns to take away from the functional programming style
that will enhance their object-oriented or procedural programming
work. If all else fails, knowledge of functional programming looks
good on your résumé!

What Is Functional Programming ?


Functional programming is a declarative programming paradigm
that abstracts code into pure, immutable, side-effect-free
functions, allowing the programmer to compose such functions
together to make programs that are easy to reason about.

That is my definition of functional programming. Ask five other


functional programmers to define functional programming and you’ll
get four more answers (two just copied the same answer from
Wikipedia). There’s no “standard” definition; different people and
different programming languages implement functional programming
elements differently. These differences are partly because of the
Random documents with unrelated
content Scribd suggests to you:
back
back
back
back
back
back
back
back
back
back
back
back
back
back
back
back
back
back

You might also like