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)

PDF
Banishing Loops with Functional Programming in PHP
David Hayes
 
PDF
Elements of Functional Programming in PHP
Jarek Jakubowski
 
PDF
Why async and functional programming in PHP7 suck and how to get overr it?
Lucas Witold Adamus
 
PDF
Iterators, ArrayAccess & Countable (Oh My!) - Madison PHP 2014
Sandy Smith
 
KEY
Can't Miss Features of PHP 5.3 and 5.4
Jeff Carouth
 
PDF
Functional Programming In PHP I
Umut IŞIK
 
PDF
Typed Properties and more: What's coming in PHP 7.4?
Nikita Popov
 
PPTX
Arrays in PHP
davidahaskins
 
PPTX
PHP = PHunctional Programming
Luis Atencio
 
PDF
Advanced Php - Macq Electronique 2010
Michelangelo van Dam
 
PDF
Object Oriented Programming in PHP
Lorna Mitchell
 
PDF
OOPs Concept
Mohammad Yousuf
 
KEY
Intermediate PHP
Bradley Holt
 
PDF
PHPID online Learning #6 Migration from procedural to OOP
Achmad Mardiansyah
 
PDF
SymfonyCon 2017 php7 performances
julien pauli
 
PPTX
PHP in 2018 - Q1 - AFUP Limoges
✅ William Pinaud
 
PDF
How to write code you won't hate tomorrow
Pete McFarlane
 
PDF
Functional programming with php7
Sérgio Rafael Siqueira
 
PDF
The most exciting features of PHP 7.1
Zend by Rogue Wave Software
 
PDF
Migration from Procedural to OOP
GLC Networks
 
Banishing Loops with Functional Programming in PHP
David Hayes
 
Elements of Functional Programming in PHP
Jarek Jakubowski
 
Why async and functional programming in PHP7 suck and how to get overr it?
Lucas Witold Adamus
 
Iterators, ArrayAccess & Countable (Oh My!) - Madison PHP 2014
Sandy Smith
 
Can't Miss Features of PHP 5.3 and 5.4
Jeff Carouth
 
Functional Programming In PHP I
Umut IŞIK
 
Typed Properties and more: What's coming in PHP 7.4?
Nikita Popov
 
Arrays in PHP
davidahaskins
 
PHP = PHunctional Programming
Luis Atencio
 
Advanced Php - Macq Electronique 2010
Michelangelo van Dam
 
Object Oriented Programming in PHP
Lorna Mitchell
 
OOPs Concept
Mohammad Yousuf
 
Intermediate PHP
Bradley Holt
 
PHPID online Learning #6 Migration from procedural to OOP
Achmad Mardiansyah
 
SymfonyCon 2017 php7 performances
julien pauli
 
PHP in 2018 - Q1 - AFUP Limoges
✅ William Pinaud
 
How to write code you won't hate tomorrow
Pete McFarlane
 
Functional programming with php7
Sérgio Rafael Siqueira
 
The most exciting features of PHP 7.1
Zend by Rogue Wave Software
 
Migration from Procedural to OOP
GLC Networks
 

Recently uploaded (20)

PPTX
ChatGPT's Deck on The Enduring Legacy of Fax Machines
Greg Swan
 
PDF
Software Development Company | KodekX
KodekX
 
PPTX
C Programming Basics concept krnppt.pptx
Karan Prajapat
 
PDF
How Onsite IT Support Drives Business Efficiency, Security, and Growth.pdf
Captain IT
 
PDF
DevOps & Developer Experience Summer BBQ
AUGNYC
 
PDF
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
PDF
CIFDAQ's Teaching Thursday: Moving Averages Made Simple
CIFDAQ
 
PDF
Make GenAI investments go further with the Dell AI Factory - Infographic
Principled Technologies
 
PPTX
How to Build a Scalable Micro-Investing Platform in 2025 - A Founder’s Guide ...
Third Rock Techkno
 
PPT
L2 Rules of Netiquette in Empowerment technology
Archibal2
 
PPTX
Comunidade Salesforce São Paulo - Desmistificando o Omnistudio (Vlocity)
Francisco Vieira Júnior
 
PDF
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
PDF
agentic-ai-and-the-future-of-autonomous-systems.pdf
siddharthnetsavvies
 
PDF
madgavkar20181017ppt McKinsey Presentation.pdf
georgschmitzdoerner
 
PDF
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
PDF
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
PDF
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
PDF
Software Development Methodologies in 2025
KodekX
 
PDF
Event Presentation Google Cloud Next Extended 2025
minhtrietgect
 
PDF
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
ChatGPT's Deck on The Enduring Legacy of Fax Machines
Greg Swan
 
Software Development Company | KodekX
KodekX
 
C Programming Basics concept krnppt.pptx
Karan Prajapat
 
How Onsite IT Support Drives Business Efficiency, Security, and Growth.pdf
Captain IT
 
DevOps & Developer Experience Summer BBQ
AUGNYC
 
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
CIFDAQ's Teaching Thursday: Moving Averages Made Simple
CIFDAQ
 
Make GenAI investments go further with the Dell AI Factory - Infographic
Principled Technologies
 
How to Build a Scalable Micro-Investing Platform in 2025 - A Founder’s Guide ...
Third Rock Techkno
 
L2 Rules of Netiquette in Empowerment technology
Archibal2
 
Comunidade Salesforce São Paulo - Desmistificando o Omnistudio (Vlocity)
Francisco Vieira Júnior
 
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
agentic-ai-and-the-future-of-autonomous-systems.pdf
siddharthnetsavvies
 
madgavkar20181017ppt McKinsey Presentation.pdf
georgschmitzdoerner
 
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
Software Development Methodologies in 2025
KodekX
 
Event Presentation Google Cloud Next Extended 2025
minhtrietgect
 
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 

PHP, Arrays & Functional Programming