class Scheduler
{
protected $actions = [];
function invoke( $func, $deadline )
{
$this->actions[] = [ "deadline" => $deadline, "func" => $func ];
}
function run()
{
while( $this->actions )
{
$action = array_shift( $this->actions ); // Pull an action to be executed
$deadline = $action["deadline"];
$func = $action["func"];
$delta = $deadline - time();
if( $delta <= 0 )
$func(); // Execute the action
else
// Reschedule the action if its deadline still in the feature
$this->actions[] = [ "deadline" => $deadline, "func" => $func ];
sleep(1);
}
}
}
function feature( $msg )
{
echo $msg;
}
$scheduler = new Scheduler();
$msg = "This content is scheduled to be executed in the feature after 7 Seconds\r\n";
$scheduler->invoke( fn() => feature( $msg ), time() + 7 ); // This feature function will be called after 7 seconds
$msg = "This content is scheduled to be executed in the feature after 2 Seconds\r\n";
$scheduler->invoke( fn() => feature( $msg ), time() + 2 ); // This feature function will be called after 4 seconds
$scheduler->run();