- var_dump: documentation ( source)
- array_reduce: documentation ( source)
- max: documentation ( source)
<?php
// SETUP CODE
class A {
function __construct($val) {
$this->val = $val;
}
}
$objs = [
new A(3),
new A(5),
new A(2),
new A(8),
new A(1),
];
// AMAZING max_by IMPL :P
$res = array_reduce($objs, function ($c, $o) {
$p = !is_null($c) ? $c->val : 0;
$c = max($o->val, $p);
return $o->val == $p ? $o : $c;
}, new A(0));
var_dump($res);