- var_dump: documentation ( source)
- usort: documentation ( source)
<?php
class Obj {
private $_name;
public function getName() {
if ($this->_name === null) {
// Heavy DB query
$this->_name = 'Foo';
}
return $this->_name;
}
}
$objects = array(new Obj(), new Obj());
usort($objects, function($a, $b) {
if ($a->getName() < $b->getName()) {
return 1;
} else if ($a->getName() > $b->getName()) {
return -1;
}
return 0;
});
var_dump($objects);