SlideShare a Scribd company logo
PHP, Arrays & Functional
Programming
4/2016
Model Driven Software Development
Sclable transforms your knowledge into
enterprise-grade, ready-to-go Business Applications.
Sclable Business Solutions GmbH
https://sclable.com/
4/2016
By the way:
We’re always looking for
Full Stack Developers
4/2016
Sclable Platform Senior Developer
PL/PGSQL - PHP - JS
← full stack →
Aviation Enthusiast
Michael Rutz
4/2016
f(x) === f(x)
Immutable
y = x; f(x); x === y;
No side effects
Functional Programming
4/2016
$first = 0;
$second = 1;
for ($i = 0; $i < 10; $i++) {
echo $first . PHP_EOL;
$tmp = $first;
$first = $second;
$second += $tmp;
}
Functional Programming
Global state has changed
4/2016
function fibonacci($n, $first = 0, $second = 1) {
if ($n === 0) return '';
return $first . PHP_EOL
. fibonacci(--$n, $second, $first + $second);
}
echo fibonacci(10);
Functional Programming
Global state untouched
Immutable
4/2016
FPHP?
Functional Programming in PHP
4/2016
Imperative design, but
Closures ✓
Functional Programming in PHP
4/2016
$ids = array_map(function ($item) {
return $item->id;
}, $list);
// js / ES2015
let ids = list.map(item => item.id);
Functional Programming in PHP
4/2016
Implemented the right way,
code readability can be improved.
Functional Programming in PHP
4/2016
https://secure.php.net/manual/en/book.array.php
Array Functions
4/2016
Performance !
Nice shorthands !
Array Functions
4/2016
$idsToLoad = array_diff($idsRequired, $idsLoaded);
// e.g. cities -> countries
$referencedCountryIds = array_unique([1,1,2,3]);
Array Functions
4/2016
array array_map(callable, array, array...)
bool array_walk(&array, callable)
array array_unique(array, sort_flags)
Inconsistent API
4/2016
https://github.com/sclable/array-functions
composer require sclable/array-functions
class ArrayWrap
4/2016
Normalized Params
OO & FP Approach
class ArrayWrap
4/2016
// e.g. cities -> countries
$referencedCountryIds = ArrayWrap::create($cities)
->map(function ($city) { return->countryId; })
->unique();
e.g.
4/2016
More e.g.
4/2016
$array = array_pad([], 10, 0);
foreach ($array as &$item) {
$item = rand();
}
// find max
$max = null;
foreach ($array as $item) {
$max = $max === null ?
$item : max($max, $item);
}
4/2016
ArrayWrap::create([])
->pad(10, 0)
->map(function () { return rand(); })
->max();
4/2016
// [x] immutable
// [x] no side effects
$emptyArr = ArrayWrap::create([]);
$padded = $emptyArr->pad(10, 0);
$randList = $padded->map( … );
$max = $randList->max();
var_dump($emptyArr === $padded); // false
var_dump($padded === $randList); // false
4/2016
foreach vs. array_map/array_walk
Performance Considerations
4/2016
! major performance impact on closures !
xDebug
4/2016
$ids = []
foreach ($models as $model) {
$ids[] = $model->id;
}
$ids = array_map(
function ($model) { return $model->id; },
$models
);
Extract a list of ids
4/2016
Foreach vs. Array_map 1:0
Extract a list of ids
4/2016
$assigned = [];
foreach ($instances as $instance) {
$assigned[$instance->id] = $instance;
}
$assigned = array_combine(array_map(
function ($instance) {return $instance->id;},
$instances
), $instances);
Assign by id
4/2016
Foreach vs. Array_map 2:0
Assign by id
4/2016
$yelled = [‘OH’, ‘SO’, ‘LOUD’];
$pssst = [];
foreach ($yelled as $v) {
$pssst[] = strtolower($v);
}
$pssst = array_map(‘strtolower’, $yelled);
Apply native functions
4/2016
Foreach vs. Array_map 2:1
Apply native functions
4/2016
Performance.
But readability!
Conclusions
4/2016
Performance.
But readability!
Conclusions
+ It looks cool!
4/2016
Thank you.
That’s all folks!

More Related Content

DOCX
One dimensional operation of Array in C- language
9096308941
 
PDF
Factorial
Ankit Dubey
 
PDF
Program in ‘C’ language to implement linear search using pointers
Dr. Loganathan R
 
PDF
Numerical Analysis lab 4
Rajon
 
DOC
basic shell_programs
madhugvskr
 
TXT
Fred
09461193
 
PDF
Bcsl 033 data and file structures lab s2-1
Dr. Loganathan R
 
One dimensional operation of Array in C- language
9096308941
 
Factorial
Ankit Dubey
 
Program in ‘C’ language to implement linear search using pointers
Dr. Loganathan R
 
Numerical Analysis lab 4
Rajon
 
basic shell_programs
madhugvskr
 
Fred
09461193
 
Bcsl 033 data and file structures lab s2-1
Dr. Loganathan R
 

What's hot (16)

TXT
Aggregate
Suresh Cse
 
ODP
Osmose-QA OpenData
Frédéric Rodrigo
 
PDF
Bcsl 033 data and file structures lab s2-2
Dr. Loganathan R
 
PDF
Odd number
Ankit Dubey
 
PDF
Bcsl 033 data and file structures lab s1-1
Dr. Loganathan R
 
PDF
Binary search
Hitesh Kumar
 
PDF
Ps installedsoftware
Elihu El, ITIL, SCRUM Master
 
DOC
Palindrome number program c
mohdshanu
 
DOCX
(Meta 5) ejemplo vectores 2 dev c++
Eli Diaz
 
DOCX
(Meta 5) ejemplo vectores dev c++
Eli Diaz
 
PDF
Advanced Crystal Reports: Techniques for compiling Annual Reports & other sta...
Chad Petrovay
 
DOCX
8.1
namthip2539
 
DOCX
WAP to initialize different objects with different values in java
One97 Communications Limited
 
PDF
Positive (2)
Ankit Dubey
 
DOCX
Euler method in c
Subir Halder
 
Aggregate
Suresh Cse
 
Osmose-QA OpenData
Frédéric Rodrigo
 
Bcsl 033 data and file structures lab s2-2
Dr. Loganathan R
 
Odd number
Ankit Dubey
 
Bcsl 033 data and file structures lab s1-1
Dr. Loganathan R
 
Binary search
Hitesh Kumar
 
Ps installedsoftware
Elihu El, ITIL, SCRUM Master
 
Palindrome number program c
mohdshanu
 
(Meta 5) ejemplo vectores 2 dev c++
Eli Diaz
 
(Meta 5) ejemplo vectores dev c++
Eli Diaz
 
Advanced Crystal Reports: Techniques for compiling Annual Reports & other sta...
Chad Petrovay
 
WAP to initialize different objects with different values in java
One97 Communications Limited
 
Positive (2)
Ankit Dubey
 
Euler method in c
Subir Halder
 
Ad

Viewers also liked (16)

PDF
SuccessFactors_WFA_Admin_Certification
Charmi Jilka
 
PDF
Reference for Pawala Ariyathilaka (2)
Pawala Ariyathilaka
 
PDF
Casos Aprobados 1543 - 08 de octubre 2014
Coordinación Académica Escuela de Educación
 
PDF
Ejercicio final de microsoft word
Jhónniier minotta
 
PPTX
Fungsi neuroendokrin
indahsen31
 
PPTX
Microservices With SenecaJS
Designveloper
 
PPTX
Abstracción geometria y proporciones
Maria Fernanda Jaimes
 
PDF
Certificados Locutor Acta 20
Facultad de Humanidades y Educación
 
PDF
Certificados Locutor Acta 6
Facultad de Humanidades y Educación
 
PDF
Certificados Locutor Acta 1
Facultad de Humanidades y Educación
 
PDF
Certificados Locutor Acta 10
Facultad de Humanidades y Educación
 
PPTX
La recherche de l'efficience - Lectra
Marketo
 
PPTX
P3O - The Value Adding PMO - from Strategy to Projects
Tony Vynckier
 
PDF
1 corinthians 13
MyWonderStudio
 
PPTX
Database Consolidation using Oracle Multitenant
Pini Dibask
 
PPT
Introduction à Twitter
Aymeric
 
SuccessFactors_WFA_Admin_Certification
Charmi Jilka
 
Reference for Pawala Ariyathilaka (2)
Pawala Ariyathilaka
 
Casos Aprobados 1543 - 08 de octubre 2014
Coordinación Académica Escuela de Educación
 
Ejercicio final de microsoft word
Jhónniier minotta
 
Fungsi neuroendokrin
indahsen31
 
Microservices With SenecaJS
Designveloper
 
Abstracción geometria y proporciones
Maria Fernanda Jaimes
 
Certificados Locutor Acta 20
Facultad de Humanidades y Educación
 
Certificados Locutor Acta 6
Facultad de Humanidades y Educación
 
Certificados Locutor Acta 1
Facultad de Humanidades y Educación
 
Certificados Locutor Acta 10
Facultad de Humanidades y Educación
 
La recherche de l'efficience - Lectra
Marketo
 
P3O - The Value Adding PMO - from Strategy to Projects
Tony Vynckier
 
1 corinthians 13
MyWonderStudio
 
Database Consolidation using Oracle Multitenant
Pini Dibask
 
Introduction à Twitter
Aymeric
 
Ad

Similar to PHP, Arrays & Functional Programming (20)

PPTX
PHP Basics
Saraswathi Murugan
 
PPTX
Tidy Up Your Code
Abbas Ali
 
PDF
PHP and Rich Internet Applications
elliando dias
 
PDF
PHP and Rich Internet Applications
elliando dias
 
PDF
Be pragmatic, be SOLID (at Boiling Frogs, Wrocław)
Krzysztof Menżyk
 
PDF
07 Introduction to PHP #burningkeyboards
Denis Ristic
 
PDF
OOP Is More Than Cars and Dogs
Chris Tankersley
 
PDF
OOP is more than Cars and Dogs
Chris Tankersley
 
PDF
Web app development_php_06
Hassen Poreya
 
DOC
The Truth About Lambdas in PHP
Sharon Levy
 
PPTX
PHP7 Presentation
David Sanchez
 
PDF
Why async and functional programming in PHP7 suck and how to get overr it?
Lucas Witold Adamus
 
PDF
GlueCon 2016 - Threading in JavaScript
Jonathan Baker
 
ODP
Clean code for WordPress
mtoppa
 
PDF
JavaScript ES6
Leo Hernandez
 
PDF
Giới thiệu PHP 7
ZendVN
 
PPT
The FPDF Library
Dave Ross
 
PDF
Why is crud a bad idea - focus on real scenarios
Divante
 
PDF
Phpspec tips&amp;tricks
Filip Golonka
 
PDF
Drupaljam xl 2019 presentation multilingualism makes better programmers
Alexander Varwijk
 
PHP Basics
Saraswathi Murugan
 
Tidy Up Your Code
Abbas Ali
 
PHP and Rich Internet Applications
elliando dias
 
PHP and Rich Internet Applications
elliando dias
 
Be pragmatic, be SOLID (at Boiling Frogs, Wrocław)
Krzysztof Menżyk
 
07 Introduction to PHP #burningkeyboards
Denis Ristic
 
OOP Is More Than Cars and Dogs
Chris Tankersley
 
OOP is more than Cars and Dogs
Chris Tankersley
 
Web app development_php_06
Hassen Poreya
 
The Truth About Lambdas in PHP
Sharon Levy
 
PHP7 Presentation
David Sanchez
 
Why async and functional programming in PHP7 suck and how to get overr it?
Lucas Witold Adamus
 
GlueCon 2016 - Threading in JavaScript
Jonathan Baker
 
Clean code for WordPress
mtoppa
 
JavaScript ES6
Leo Hernandez
 
Giới thiệu PHP 7
ZendVN
 
The FPDF Library
Dave Ross
 
Why is crud a bad idea - focus on real scenarios
Divante
 
Phpspec tips&amp;tricks
Filip Golonka
 
Drupaljam xl 2019 presentation multilingualism makes better programmers
Alexander Varwijk
 

Recently uploaded (20)

PDF
CIFDAQ's Token Spotlight: SKY - A Forgotten Giant's Comeback?
CIFDAQ
 
PDF
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
PDF
Event Presentation Google Cloud Next Extended 2025
minhtrietgect
 
PPTX
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
sujalchauhan1305
 
DOCX
Top AI API Alternatives to OpenAI: A Side-by-Side Breakdown
vilush
 
PDF
Unlocking the Future- AI Agents Meet Oracle Database 23ai - AIOUG Yatra 2025.pdf
Sandesh Rao
 
PPTX
How to Build a Scalable Micro-Investing Platform in 2025 - A Founder’s Guide ...
Third Rock Techkno
 
PDF
Enable Enterprise-Ready Security on IBM i Systems.pdf
Precisely
 
PDF
Building High-Performance Oracle Teams: Strategic Staffing for Database Manag...
SMACT Works
 
PDF
Cloud-Migration-Best-Practices-A-Practical-Guide-to-AWS-Azure-and-Google-Clou...
Artjoker Software Development Company
 
PDF
The Evolution of KM Roles (Presented at Knowledge Summit Dublin 2025)
Enterprise Knowledge
 
PDF
madgavkar20181017ppt McKinsey Presentation.pdf
georgschmitzdoerner
 
PDF
A Day in the Life of Location Data - Turning Where into How.pdf
Precisely
 
PDF
Make GenAI investments go further with the Dell AI Factory - Infographic
Principled Technologies
 
PDF
Revolutionize Operations with Intelligent IoT Monitoring and Control
Rejig Digital
 
PPTX
ChatGPT's Deck on The Enduring Legacy of Fax Machines
Greg Swan
 
PDF
DevOps & Developer Experience Summer BBQ
AUGNYC
 
PDF
REPORT: Heating appliances market in Poland 2024
SPIUG
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PDF
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
CIFDAQ's Token Spotlight: SKY - A Forgotten Giant's Comeback?
CIFDAQ
 
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
Event Presentation Google Cloud Next Extended 2025
minhtrietgect
 
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
sujalchauhan1305
 
Top AI API Alternatives to OpenAI: A Side-by-Side Breakdown
vilush
 
Unlocking the Future- AI Agents Meet Oracle Database 23ai - AIOUG Yatra 2025.pdf
Sandesh Rao
 
How to Build a Scalable Micro-Investing Platform in 2025 - A Founder’s Guide ...
Third Rock Techkno
 
Enable Enterprise-Ready Security on IBM i Systems.pdf
Precisely
 
Building High-Performance Oracle Teams: Strategic Staffing for Database Manag...
SMACT Works
 
Cloud-Migration-Best-Practices-A-Practical-Guide-to-AWS-Azure-and-Google-Clou...
Artjoker Software Development Company
 
The Evolution of KM Roles (Presented at Knowledge Summit Dublin 2025)
Enterprise Knowledge
 
madgavkar20181017ppt McKinsey Presentation.pdf
georgschmitzdoerner
 
A Day in the Life of Location Data - Turning Where into How.pdf
Precisely
 
Make GenAI investments go further with the Dell AI Factory - Infographic
Principled Technologies
 
Revolutionize Operations with Intelligent IoT Monitoring and Control
Rejig Digital
 
ChatGPT's Deck on The Enduring Legacy of Fax Machines
Greg Swan
 
DevOps & Developer Experience Summer BBQ
AUGNYC
 
REPORT: Heating appliances market in Poland 2024
SPIUG
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 

PHP, Arrays & Functional Programming