PHP Timer

By sunslayer on Mar 19, 2011

similar to mIRC's /timer alias

<?php
    class timer {

        public $callBack, $i, $t;

        public function __construct($i,$t) {
            $this->i = $i;
            $this->t = $t;
            if($i*$t > ini_get('max_execution_time') || $t < 1) {
                trigger_error("max_execution_time will prevent timer from functioning completely", E_USER_WARNING);
                flush();
            }
        }

        public function add($cb,array $params=null) {
            if((@function_exists($cb)&&is_string($cb))||is_array($cb)&&count($cb)==2&&@method_exists($cb[0],$cb[1]))
                $this->callBack[] = array("f" => $cb, "p" => $params);
        }

        public function exec() {
            do {
                foreach($this->callBack as $cb) {
                    if(is_array($cb['f']))
                        $cb['f'] = implode("::",$cb['f']);
                    call_user_func_array($cb['f'],(array)$cb['p']);
                }
                $this->t--;
                flush();
                sleep($this->i);
            }   while($this->t != 0);
        }

        public function __toString() {
            return __CLASS__;
        }
    }
?>

Comments

Sign in to comment.
ProIcons   -  Mar 27, 2011

Good job :) Perfect code syntax 9/10 :) I Like it!

 Respond  
Hawkee   -  Mar 21, 2011

sunslayer, interesting idea for the RSS feed reader. I'd love to see you post that someday.

 Respond  
[Plornt]   -  Mar 21, 2011

Shame PHP isnt multithreaded(is that the word here?) since this (at a glance) would just literally stop the script while it waited, perhaps you could make it so you could add it to a external job queue of which another script would pick it up and individually proccess the timers?

 Respond  
sunslayer   -  Mar 21, 2011

this has many uses, checking your email/rss feeds, and(what I'm using for) can be combine with GTK to create timer-like jobs in a gui setting

 Respond  
Hawkee   -  Mar 21, 2011

Won't this simply stop the code from executing while it waits for the next call? sleep() doesn't allow for asynchronous callbacks so you can't really execute anything while you're waiting for the timer to play out. I'm just not sure what practical application this has.

 Respond  
sunslayer   -  Mar 19, 2011

it follows the same basic syntax as mIRC, timer(interval, repitition) where interval is the interval between executions and repitition is the number of times its repeated(n<1 for infinite), also you can queue multiple callbacks to one timer by using add() method, i forgot to mention it in the example but you can also pass parameters in an array as a second param for add(). exec() simply executes the timer

it also throws an error if you create a timer that will take longer than max_execution_time allows the script to run for

 Respond  
Conscious   -  Mar 19, 2011

How does that even work? :o

 Respond  
sunslayer   -  Mar 19, 2011

heres an example

<?php
    function examp() {
        echo "Hello ";
    }
    class example {
        static public function E() {
            echo "World!\n";
        }
    }

    $timer = new timer(2,5);
    $timer->add("examp");
    $timer->add(array("example","E"));
    $timer->exec();
?>
 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.