From b3cc13d4f277980b643a6c02d3981c24b524e848 Mon Sep 17 00:00:00 2001 From: Shoghi Cervantes Pueyo Date: Sat, 29 Dec 2012 18:06:10 +0100 Subject: [PATCH] Async class --- src/classes/Async.class.php | 69 +++++++++++++++++++++++++++++++++++++ src/common/dependencies.php | 2 +- 2 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 src/classes/Async.class.php diff --git a/src/classes/Async.class.php b/src/classes/Async.class.php new file mode 100644 index 00000000..26b4c98f --- /dev/null +++ b/src/classes/Async.class.php @@ -0,0 +1,69 @@ +method = $method; + $this->params = $params; + $this->result = null; + $this->joined = false; + } + + /** + * The smallest thread in the world + **/ + public function run(){ + if (($this->result=call_user_func_array($this->method, $this->params))) { + return true; + } else return false; + } + + /** + * Static method to create your threads from functions ... + **/ + public static function call($method, $params){ + $thread = new Async($method, $params); + if($thread->start()){ + return $thread; + } /** else throw Nastyness **/ + } + + /** + * Do whatever, result stored in $this->result, don't try to join twice + **/ + public function __toString(){ + if(!$this->joined) { + $this->joined = true; + $this->join(); + } + + return $this->result; + } +} \ No newline at end of file diff --git a/src/common/dependencies.php b/src/common/dependencies.php index 886c9ce8..5c0408aa 100644 --- a/src/common/dependencies.php +++ b/src/common/dependencies.php @@ -74,7 +74,7 @@ if($errors > 0){ } - +require_once("classes/Async.class.php"); require_once("classes/Data.class.php"); require_once("classes/Player.class.php"); require_once("classes/Generator.class.php");