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

What's hot (16)

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

Viewers also liked (16)

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

Similar to PHP, Arrays & Functional Programming (20)

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

Recently uploaded (20)

ENERGY CONSUMPTION CALCULATION IN ENERGY-EFFICIENT AIR CONDITIONER.pdf
ENERGY CONSUMPTION CALCULATION IN ENERGY-EFFICIENT AIR CONDITIONER.pdf
Muhammad Rizwan Akram
 
Data Validation and System Interoperability
Data Validation and System Interoperability
Safe Software
 
Mastering AI Workflows with FME - Peak of Data & AI 2025
Mastering AI Workflows with FME - Peak of Data & AI 2025
Safe Software
 
“Addressing Evolving AI Model Challenges Through Memory and Storage,” a Prese...
“Addressing Evolving AI Model Challenges Through Memory and Storage,” a Prese...
Edge AI and Vision Alliance
 
Analysis of the changes in the attitude of the news comments caused by knowin...
Analysis of the changes in the attitude of the news comments caused by knowin...
Matsushita Laboratory
 
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
AmirStern2
 
Your startup on AWS - How to architect and maintain a Lean and Mean account J...
Your startup on AWS - How to architect and maintain a Lean and Mean account J...
angelo60207
 
Oracle Cloud and AI Specialization Program
Oracle Cloud and AI Specialization Program
VICTOR MAESTRE RAMIREZ
 
War_And_Cyber_3_Years_Of_Struggle_And_Lessons_For_Global_Security.pdf
War_And_Cyber_3_Years_Of_Struggle_And_Lessons_For_Global_Security.pdf
biswajitbanerjee38
 
FIDO Seminar: New Data: Passkey Adoption in the Workforce.pptx
FIDO Seminar: New Data: Passkey Adoption in the Workforce.pptx
FIDO Alliance
 
FME for Distribution & Transmission Integrity Management Program (DIMP & TIMP)
FME for Distribution & Transmission Integrity Management Program (DIMP & TIMP)
Safe Software
 
vertical-cnc-processing-centers-drillteq-v-200-en.pdf
vertical-cnc-processing-centers-drillteq-v-200-en.pdf
AmirStern2
 
June Patch Tuesday
June Patch Tuesday
Ivanti
 
Agentic AI: Beyond the Buzz- LangGraph Studio V2
Agentic AI: Beyond the Buzz- LangGraph Studio V2
Shashikant Jagtap
 
Your startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean account
angelo60207
 
Edge-banding-machines-edgeteq-s-200-en-.pdf
Edge-banding-machines-edgeteq-s-200-en-.pdf
AmirStern2
 
Providing an OGC API Processes REST Interface for FME Flow
Providing an OGC API Processes REST Interface for FME Flow
Safe Software
 
Down the Rabbit Hole – Solving 5 Training Roadblocks
Down the Rabbit Hole – Solving 5 Training Roadblocks
Rustici Software
 
Murdledescargadarkweb.pdfvolumen1 100 elementary
Murdledescargadarkweb.pdfvolumen1 100 elementary
JorgeSemperteguiMont
 
FIDO Seminar: Targeting Trust: The Future of Identity in the Workforce.pptx
FIDO Seminar: Targeting Trust: The Future of Identity in the Workforce.pptx
FIDO Alliance
 
ENERGY CONSUMPTION CALCULATION IN ENERGY-EFFICIENT AIR CONDITIONER.pdf
ENERGY CONSUMPTION CALCULATION IN ENERGY-EFFICIENT AIR CONDITIONER.pdf
Muhammad Rizwan Akram
 
Data Validation and System Interoperability
Data Validation and System Interoperability
Safe Software
 
Mastering AI Workflows with FME - Peak of Data & AI 2025
Mastering AI Workflows with FME - Peak of Data & AI 2025
Safe Software
 
“Addressing Evolving AI Model Challenges Through Memory and Storage,” a Prese...
“Addressing Evolving AI Model Challenges Through Memory and Storage,” a Prese...
Edge AI and Vision Alliance
 
Analysis of the changes in the attitude of the news comments caused by knowin...
Analysis of the changes in the attitude of the news comments caused by knowin...
Matsushita Laboratory
 
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
AmirStern2
 
Your startup on AWS - How to architect and maintain a Lean and Mean account J...
Your startup on AWS - How to architect and maintain a Lean and Mean account J...
angelo60207
 
Oracle Cloud and AI Specialization Program
Oracle Cloud and AI Specialization Program
VICTOR MAESTRE RAMIREZ
 
War_And_Cyber_3_Years_Of_Struggle_And_Lessons_For_Global_Security.pdf
War_And_Cyber_3_Years_Of_Struggle_And_Lessons_For_Global_Security.pdf
biswajitbanerjee38
 
FIDO Seminar: New Data: Passkey Adoption in the Workforce.pptx
FIDO Seminar: New Data: Passkey Adoption in the Workforce.pptx
FIDO Alliance
 
FME for Distribution & Transmission Integrity Management Program (DIMP & TIMP)
FME for Distribution & Transmission Integrity Management Program (DIMP & TIMP)
Safe Software
 
vertical-cnc-processing-centers-drillteq-v-200-en.pdf
vertical-cnc-processing-centers-drillteq-v-200-en.pdf
AmirStern2
 
June Patch Tuesday
June Patch Tuesday
Ivanti
 
Agentic AI: Beyond the Buzz- LangGraph Studio V2
Agentic AI: Beyond the Buzz- LangGraph Studio V2
Shashikant Jagtap
 
Your startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean account
angelo60207
 
Edge-banding-machines-edgeteq-s-200-en-.pdf
Edge-banding-machines-edgeteq-s-200-en-.pdf
AmirStern2
 
Providing an OGC API Processes REST Interface for FME Flow
Providing an OGC API Processes REST Interface for FME Flow
Safe Software
 
Down the Rabbit Hole – Solving 5 Training Roadblocks
Down the Rabbit Hole – Solving 5 Training Roadblocks
Rustici Software
 
Murdledescargadarkweb.pdfvolumen1 100 elementary
Murdledescargadarkweb.pdfvolumen1 100 elementary
JorgeSemperteguiMont
 
FIDO Seminar: Targeting Trust: The Future of Identity in the Workforce.pptx
FIDO Seminar: Targeting Trust: The Future of Identity in the Workforce.pptx
FIDO Alliance
 

PHP, Arrays & Functional Programming