* @package Mammut\Util */ class LazyLoader extends \Mammut\StdObject { private $instance = NULL; private $known = false; private $initFunc; private $param = false; /** * Creates a new helper instance * * @param \Closure $initFunc * the function which creates the new object */ public function __construct(\Closure $initFunc) { $this->initFunc = $initFunc; } public function setParam(array $p) { $this->param = $p; } public function getParam() { return $this->param; } /** * Receives the value. * Calles the defined function if the current known value is unknown. * * @return mixed the result of the function call */ public function get() { if(!$this->known) { $f = $this->initFunc; $this->instance = ($this->param) ? $f($this->param) : $f(); $this->known = true; } return $this->instance; } /** * Removes the known value and forces the fetch with a function call */ public function clear() { $this->instance = NULL; $this->known = false; } }