Created
April 12, 2011 21:20
-
-
Save xeoncross/916439 to your computer and use it in GitHub Desktop.
Auto Object Loading
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class o | |
{ | |
private $o; | |
// Get value | |
public function __get($k){return isset($this->o[$k])?$this->o[$k]:NULL;} | |
// Create or Get value | |
public function __call($k,$a) | |
{ | |
if(!isset($this->o[$k])){$c=count($a);$this->o[$k]=$c==1?new$k($a[0]):($c==2?new$k($a[0],$a[1]):($c==3?new$k($a[0],$a[1],$a[2]):($c==4?new$k($a[0],$a[1],$a[2],$a[3]):new$k($a))));}return$this->o[$k]; | |
} | |
public function clear($k=NULL){$this->o=array();} | |
public function __set($k,$v){$this->o[$k]=$v;} | |
public function __isset($k){return isset($this->o[$k]);} | |
public function __unset($k){unset($this->o[$k]);} | |
} | |
function o(){static$o;return$o?$o:($o=new o);} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* If you are loading a singleton object then you need to use __get which | |
* will run the command once and save the object. | |
* | |
* If you are using a factory or lambda function then you need to use __call() | |
*/ | |
class Service | |
{ | |
protected $s = array(); | |
function __set($key, $callable) | |
{ | |
$this->s[strtolower($key)] = $callable; | |
} | |
function __get($key) | |
{ | |
if( ! isset($this->s[$key = strtolower($key)])) | |
{ | |
throw new Exception("$key service not found"); | |
} | |
if($this->s[$key] instanceof Closure) | |
{ | |
$this->s[$key] = $this->s[$key]($this); | |
} | |
return $this->s[$key]; | |
} | |
function __isset($key) | |
{ | |
return isset($this->s[strtolower($key)]); | |
} | |
function __unset($key) | |
{ | |
unset($this->s[strtolower($key)]); | |
} | |
function __call($key, $args) | |
{ | |
if( ! isset($this->s[$key = strtolower($key)])) | |
{ | |
throw new Exception("$key service not found"); | |
} | |
if(! $args) return $this->s[$key]($this); | |
//print dump("Calling user_func_array for $key"); | |
array_unshift($args, $this); | |
return call_user_func_array($this->s[$key], $args); | |
} | |
public function observe($method, $params = NULL) | |
{ | |
foreach($this->s as $observer) | |
{ | |
if($observer instanceof Observer) | |
{ | |
$params = $observer->observe($this, $method, $params); | |
} | |
} | |
return $params; | |
} | |
} | |
abstract class Observer | |
{ | |
public function __invoke($subject, $method, $params = NULL){} | |
public function attached($subject){} | |
public function detached($subject){} | |
} | |
function s($kill = FALSE) | |
{ | |
static$s;if($kill)$s=NULL;return$s?:($s=new Service); | |
} | |
function benchmark($text = '') | |
{ | |
static $t,$m; | |
if(!$t){$t=microtime(TRUE);$m=memory_get_usage();return;} | |
if($text)print dump($text); | |
print dump('Memory: '.(memory_get_usage()-$m)."\n".(microtime(TRUE)-$t)); | |
} | |
benchmark(); | |
$t = microtime(TRUE); | |
$m = memory_get_usage(); | |
function dump() | |
{ | |
$string = ''; | |
foreach(func_get_args() as $value) | |
{ | |
$string .= '<pre>' . ($value === NULL ? 'NULL' : (is_scalar($value) ? $value : print_r($value, TRUE))) . "</pre>\n"; | |
} | |
return $string; | |
} | |
class D { | |
public function __construct($s) { print dump('Loaded '. __CLASS__); } | |
public function speak($word = 'HI') { print dump($word); } | |
} | |
s()->a = function($s, $user, $pass) { print dump('A' . $user . $pass); }; | |
s()->b = function($s) { print dump('B'); }; | |
s()->c = function() { print dump('C'); }; | |
s()->d = function($s) { return new D($s); }; | |
benchmark(); | |
s()->a('hello', 'world'); | |
benchmark(); | |
s()->b(); | |
s()->c(); | |
s()->d()->speak(); | |
s()->d->speak(); | |
//s()->d()->speak(); // WRONG! This closure has been converted to an object now! | |
s()->d->speak('something'); | |
for ($i = 0; $i < 1000; $i++) | |
{ | |
s()->$i = function($s) { return new D($s); }; // waste more functions to increase iteration count | |
s()->d; | |
} | |
benchmark(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment