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!

PHP, Arrays & Functional Programming