The most typical usage is with stack, queue or collection, for example when you queue tasks, make call stack or manipulate JSON, XML, etc. elements.
As other exepctions of RuntimeException class, this type of error can't be detected in (for example) your IDE or compiler.
<?php
$f1 = function() { setTypeControl('username');};
$f2 = function() { setTypeControl('userpass');};
$f3 = function() { setButton('Add');};
$f4 = function() { setButton('OK');};
$tasks = new class {
private $list;
public function __construct() {
$this->list = new SplQueue;
}
public function add(callable $func) {
$this->list->enqueue($func);
}
public function do() {
if ($this->list->isEmpty()) {
throw new UnderflowException;
} else {
call_user_func($this->list->dequeue());
}
}
};
$tasks->add($f1);
$tasks->add($f2);
$tasks->add($f3);
$tasks->add($f4);
$tasks->do(); $tasks->do(); $tasks->do(); $tasks->do(); $tasks->do();