This component is responsible for creating timers and intervals.


  • PHP ^8.1


Library is available as composer repository and can be installed using the following command in a root of your project.

  • composer require bic-engine/timer


There are two classes for creating deferred tasks:

  • Bic\Timer\Interval - Runs the task continuously once in the allotted time until the Interval is stopped.
  • Bic\Timer\Timer - Executes a task deferred once after a certain amount of time.

To create a deferred task, it must be passed to the first argument of the constructor. The second argument is responsible for the time (in seconds) after which the task will be completed.

$timer = new Bic\Timer\Interval(function () {
    echo "tick \n";
}, 4.5);

There are two ways to start the timer:

  • In blocking mode: The code will not be executed further until the timer has finished running.
  • In asynchronous mode: In this case, you yourself can control when you should call the timer to check for the fact that it needs to complete its task.
// 1) Blocking Mode

// 2) Async Mode
$fiber = new \Fiber($timer->start(...));

while (!$fiber->isTerminated()) {
    $fiber->resume(); // Execute the timer

{info} Please note that to run multiple timers, you should call them one by one.

$timer1 = new Bic\Timer\Interval(fn () => print "tick #1 \n", 1.5);
$fiber1 = new \Fiber($timer1->start(...));

$timer2 = new Bic\Timer\Interval(fn () => print "tick #2 \n", 0.5);
$fiber2 = new \Fiber($timer2->start(...));

$timers = [$fiber1, $fiber2];

while ($timers !== []) {
    foreach ($timers as $index => $timer) {
        if ($timer->isTerminated()) {
            // Remove completed timer