PHP 具备一些动态语言的特征, 但不彻底. 虽然 PHP 的标志是一头大象, 可这头象的鼻子未免太短, 以致经常够不着东西, 反而象猪了. 本文旨在探讨一种使 PHP 更动态化的方法, 主要是模拟 Javascript 的 prototype 继承. 既然是模拟, 就不是真的能使 PHP 动态起来, 只是插上一根葱, 让它装得更"象"一点.
一. 基本操作
通过 Javascript 的 prototype 动态地为对象添加属性, 我们可以这样:
Object.prototype.greeting = 'Hello' var o = new Object alert(o.greeting)
error_reporting(E_ALL); class Object { public static $prototype; protected function __get($var) { if ( isset(self::$prototype->$var) ) { return self::$prototype->$var; }} }
Object::$prototype->greeting = 'Hello'; $o = new Object; echo $o->greeting; // 输出 Hello
Object.prototype.say = function(word) { alert(word) } o.say('Hi')
error_reporting(E_ALL); class Object { public static $prototype; protected function __get($var) { if ( isset(self::$prototype->$var) ) { return self::$prototype->$var; }} protected function __call($call, $params) { if ( isset(self::$prototype->$call) && is_callable(self::$prototype->$call) ) { return call_user_func_array(self::$prototype->$call, $params); } else { throw new Exception('Call to undefined method: ' . __CLASS__ . "::$call()"); }} }
Object::$prototype->say = create_function('$word', 'echo $word;'); $o->say('Hi');
Object.prototype.rock = function() { alert(this.oops) } o.oops = 'Oops' o.rock()
Object::$prototype->rock = create_function('', 'echo $this->oops;'); $o->oops = 'Oops'; $o->rock();
Object::$prototype->rock = create_function('$caller', 'echo $caller->oops;'); $o->oops = 'Oops'; $o->rock($o);
function create_method($args, $code) { if ( preg_match('/\$that\b/', $args) ) { throw new Exception('Using reserved word \'$that\' as argument'); } $args = preg_match('/^\s*$/s', $args) ? '$that' : '$that, '. $args; $code = preg_replace('/\$this\b/', '$that', $code); return create_function($args, $code); }
class Object { public static $prototype; protected function __get($var) { if ( isset(self::$prototype->$var) ) { return self::$prototype->$var; }} protected function __call($call, $params) { if ( isset(self::$prototype->$call) && is_callable(self::$prototype->$call) ) { array_unshift($params, $this); // 这里! return call_user_func_array(self::$prototype->$call, $params); } else { throw new Exception('Call to undefined method: ' . __CLASS__ . "::$call()"); }} }
Object::$prototype->rock = create_method('', 'echo $this->oops;'); $o->oops = 'Oops'; $o->rock();
class Object { public static $prototype; protected function __get($var) { ... } protected function __call($call, $params) { ... } } class Test extends Object { } Test::$prototype->greeting = 'Hello'; print_r(Object::$prototype); /* outputs stdClass Object ( [greeting] => Hello ) */ Test::$prototype->say = create_method('$word', 'echo $word;'); $o = new Object; $o->say('Hi'); /* outputs Hi */