PHP Classes

File: README.md

Recommend this page to a friend!
  Classes of Mohammed Al Ashaal   TIX PHP Event Loop   README.md   Download  
File: README.md
Role: Example script
Content type: text/markdown
Description: Example script
Class: TIX PHP Event Loop
Run tasks added to a queue calling them in a loop
Author: By
Last change:
Date: 5 years ago
Size: 1,124 bytes
 

Contents

Class file image Download

Tix

A super simple stupid event-loop written in PHP :D

Why

JUST FOR FUN

Usage

$ chmod +x tix

$ echo "<?php echo 'Hello World';" > hello.tix

$ ./tix hello.tix
> Hello World

Timer Module?


<?php
class Timer {
    protected $start;

    function __construct() {
        $this->start = time();
    }

    function setInterval($fn, $intval) {
        $self = $this;
        $call = function() use($self, $fn, $intval, &$call) {
            $now = time();
            $diff = ($now - $this->start);
            if ( $diff >= $intval ) {
                $this->start = time();
                $fn();
            }
            tixify($call);
        };
        tixify($call);
    }
};

return new class {
    function setInterval($fn, $intval) {
        return (new Timer())->setInterval($fn, $intval);
    }
};

example.tix:

<?php

    // import "timer.tix" as "timer"
    $this->import("timer.tix", "timer");

    // run a function each 2 seconds
    $this->timer->setInterval(function(){
        echo time() . "\n";
    }, 2);

$ ./tix example.tix