当前位置:首页> PHP教程> 经典文章
关键字
文章内容
给猪的鼻子插一根葱
 
 
修改时间:[2012/01/01 21:48]    阅读次数:[1117]    发表者:[起缘]
 
PHP 具备一些动态语言的特征, 但不彻底. 虽然 PHP 的标志是一头大象, 可这头象的鼻子未免太短, 以致经常够不着东西, 反而象猪了. 本文旨在探讨一种使 PHP 更动态化的方法, 主要是模拟 Javascript 的 prototype 继承. 既然是模拟, 就不是真的能使 PHP 动态起来, 只是插上一根葱, 让它装得更"象"一点.

一. 基本操作
通过 Javascript 的 prototype 动态地为对象添加属性, 我们可以这样:
Object.prototype.greeting = 'Hello'
var o = new Object
alert(o.greeting)

Js 的内置对象 Object 可看作一个"类", 任何 Js "类"都有 prototype 内置对象, 用 PHP 来模拟它可以是:
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

这里利用了 PHP 的自动转型特性. 在 PHP 中, 我们要声明一个数组, 并不需要先 $var = array() 然后才做 $var[] = some_value, 直接地使用后者就可以得到一个数组; 同样地直接 $object->var 的时候, $object 就被自动定义为 stdClass 对象. 这就解决了在定义类内静态属性时不能声明 public static $prototype = new stdClass 的问题.

在 Js 中给"类"动态添加方法:
Object.prototype.say = function(word) { alert(word) }
o.say('Hi')

在 PHP 中模拟:
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');

但是 PHP 的 create_function 返回的结果并不等同于 Js 中的 Function 对象, Js 的 Function 对象是一种闭包(closure), 它可以直接调用宿主的属性, 如
Object.prototype.rock = function() { alert(this.oops) }
o.oops = 'Oops'
o.rock()

但是在 PHP 中我们不可以写
Object::$prototype->rock = create_function('', 'echo $this->oops;');
$o->oops = 'Oops';
$o->rock();

会报告 Fatal error: Using $this when not in object context, 因为 create_function 返回的是匿名的普通函数, 它没有宿主. 为解决这个问题, 我们需要在参数中传入对象本身, 而且不能使用 $this 变量名做参数, 我们暂时用一个 $caller 的变量名:
Object::$prototype->rock = create_function('$caller', 'echo $caller->oops;');
$o->oops = 'Oops';
$o->rock($o);

现在可以了, 可是看上去怪怪的, 一点都不像动态语言. 嗯~, 这根葱还是有点短, 还是不"象".

问题来了:
1. 在调用动态方法时需要传递对象本身, 这算哪门子的面向对象?
2. 我们要在代码中使用 $this, 这才象是在面向对象.

解决方法:
1. 重新写一个函数代替 create_function, 在参数部分挤一个参数 $that 进去作为第一个参数, 在 __call 中向匿名函数传递参数时加入对象本身 $this 作为第一参数.
2. 允许在代码中使用 $this, 我们在代替函数中把 $this 换成 $that.

我们给它添加一个 create_method 函数来代替 create_function
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); }

$that 作为参数中的"保留字", 当出现在参数部分中将抛出异常.(在 PHP5 的早期暗夜版本中, $that 也曾经是保留字)

相应地, Object 中的 __call 也要作出改动
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();





二. 继承
面向对象的一大特征是继承, 继承最大限度地保留代码重用能力. 但如果直接用上例的 Object 类去创建继承类则会出错, 因为
1. 子类继承的静态属性 $prototype 永远属于父类(不管 $prototype 是标量还是列表, 对象更不消说)
2. 如果子类所继承的方法中有 self 关键字, self 会指向父类而非子类
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
*/

总而言之, 一切类的静态属性都归声明该属性的类本身所拥有, 继承它的子类如果想拥有同样的静态属性必须在子类中重新声明. 因此, 我们需要把一切静态属性从父类中分离出来, 它们是 $prototype, self 和 __CLASS__. self 将用类名来代替, 使 self::$prototype 变成 Test::$prototype.
abstract class Object
{
     protected function __get($var) {
         $self = get_class($this);
         $prototype = eval("return $self::\$prototype;");
         if ( isset($prototype->$var) ) {
             return $prototype->$var; }}

     protected function __call($call, $params) {
         $self = get_class($this);
         $prototype = eval("return $self::\$prototype;");
         if ( isset($prototype->$call) && is_callable($prototype->$call) ) {
             array_unshift($params, $this);
             return call_user_func_array($prototype->$call, $params); }
         else {
             throw new Exception("Call to undefined method: $self::$call()"); }}
}

class Test extends Object
{
     public static $prototype;
}

class SubTest extends Test
{
}

让祖类 Object 成为抽象类, 而在直接继承 Object 的 Test 中定义再去 $prototype, 它将成为所有继承 Test 的子类的原型, 所以 Test 的子类 SubTest 及其孙类, 曾孙类等就不需再定义 $prototype 了, 也就是说 Test::$prototype 将是 Test 所有的子类孙类曾孙类所共同享有的属性, 这和 Javascript 的继承机制一致.




三. 数组问题
先看一段代码
Test::$prototype->arr = array('a' => 'Hello');
$t = new Test;
$t->arr['a'] = 'Hi';
print_r(Test::$prototype);
/* outputs:
stdClass Object
(
    [arr] => Array
        (
            [a] => Hi
        )

)
*/

Test::$prototype->arr['a'] 的值应该是 Hello, 我们要改变的是 $t->arr['a'] 的值, 但是很不幸, Test::$prototype->arr['a'] 的值被改变了. 这是 PHP 的一个 bug, 详细讨论见 http://club.phpe.net/index.php?act=ST&f=16&t=12120&s= . 我已向 bugs.php.net 提交了这个 bug, 至今没有音讯. 在此问题未解决之前, 我们必须采用一个折衷的办法, 在执行此类操作之前先
$t->arr = $t->arr;

然后再
$t->arr['a'] = 'Hi';

这样就不会影响到 Test::$prototype->arr.

补充:
在 Javascript 中, 字符串和整数, 浮点数是基本的数据类型, 它们不以引用传递, 而数组是对象, 是按引用传递的, 因此同样的情况也发生在 Js 中:
Object.prototype.arr = ['Hello']
var o = new Object
o.arr[0] = 'Hi'
alert(Object.prototype.arr[0]) // 输出 Hi

那么上述"数组问题"也许不能完全算是 bug, 虽然在 PHP 中数组不是对象, 在这里却表现出对象的特征, 不知是好事还是坏事? 如果数组被当成对象, 那么当执行了 $t->arr = $t->arr 之后, 任何对 $t->arr 内部元素的操作依然会影响到 Test::$prototype->arr, 然而却没有. 这依然还是 bug. 总之 PHP 在 __get, __set 的操作上目前还是不很理想, 程序员自己需要很清楚这两种重载的内部机理.




四. 注意事项
请注意, 我们是在模拟 prototype 继承, 所有的动态方法都不是真正的动态方法, 它们只是普通的匿名函数, 因此你无法在这些"动态方法"中调用任何对象自身的私有属性和私有方法.

葱毕竟是葱, 不是真正的鼻子. ~(^oo^)~ >>>>>> ~(^qp^)~




五. 附录
A. 为对象动态添加方法
为对象动态添加方法很简单, 仍然使用上面的 create_method 函数, 对 Object 祖类的 __call 作小小修改就可以
abstract class Object
{
     protected function __get($var) {
         ... }}

     protected function __call($call, $params) {
         $self = get_class($this);
         $prototype = eval("return $self::\$prototype;");
         if ( isset($this->$call) && is_callable($this->$call) ) { // 这里!
             array_unshift($params, $this);
             return call_user_func_array($this->$call, $params); }
         elseif ( isset($prototype->$call) && is_callable($prototype->$call) ) {
             array_unshift($params, $this);
             return call_user_func_array($prototype->$call, $params); }
         else {
             throw new Exception("Call to undefined method: $self::$call()"); }}
}

测试一下:
class Test extends Object
{
     public static $prototype;
}

Test::$prototype->greeting = 'Hello';
$t = new Test;
$t->say = create_method('$word', '
    echo $word;
');
$t->say($t->greeting); // 输出 Hello


B. 复制动态方法给其它对象
仍用上面的 Test 对象 $t
class TestAagain extends Object
{
     public static $prototype;
}

$t2 = new TestAagain;
$t2->say = $t->say;
$t2->say('Hi'); // 输出 Hi

把类的动态方法复制给其它类或对象和复制动态方法给其它对象的方法相同, 如 B::$prototype->method = A::$prototype->method, 甚至可以专门创建一个类来做原型, 如 C::$prototype = new MagicPrototype, 这在 Js 开发中是常见的, 下面我们也来试一下是否可行.

C. 用一个类来做原型
仍然要对祖类 Object 的 __call 做点改变, 这里我们要用 method_exists 检验方法是否存在:
abstract class Object
{
     protected function __get($var) {
         ... }

     protected function __call($call, $params) {
         $self = get_class($this);
         $prototype = eval("return $self::\$prototype;");
         if ( isset($this->$call) && is_callable($this->$call) ) {
             array_unshift($params, $this);
             return call_user_func_array($this->$call, $params); }
         elseif ( isset($prototype->$call) && is_callable($prototype->$call) ) {
             array_unshift($params, $this);
             return call_user_func_array($prototype->$call, $params); }
         elseif ( method_exists($prototype, $call) ) { // 这里!
             return call_user_func_array(array(&$prototype, $call), $params); }
         else {
             throw new Exception("Call to undefined method: $self::$call()"); }}
}

在这里不需要向被调用的方法传递对象了, 在类 MagicPrototype (见下文)的语境里, $this 指的是 MagicPrototype 类对象也就是 $prototype 本身. 下面仍用 Test 类来测试一下:
class Test extends Object
{
     public static $prototype;
}

class MagicPrototype
{
     public function perform_magic() {
         // 这里的 $this 可是真的哦, 它指向 $prototype 本身
         return $this->magic = 'I love magic!'; }
}

Test::$prototype = new MagicPrototype;
$t = new Test;
$t->perform_magic(); // 这里执行了 Test::$prototype->perform_magic()
echo $t->magic;      // 这里返回了 Test::$prototype->magic

由于 PHP 已经具备不错的面向对象机制, 我们也大可不必矫枉过正, 一般情况下还是在类内定义方法为好. 但是作为一种有益的补充, 这样也可以找到通往目的地的另一条路径, 大概这就是编程的乐趣吧.




六. 后记
本文受益于近来 club.phpe.net 的朋友们广泛讨论的对象动态化话题, 个人从中吸取了不少有益的观点, 和自己过去所作的各种尝试互相印证, 取长补短乃成. 顺此致谢!

原文参见:http://club.phpe.net/index.php?act=ST&f=15&t=12629

(完)

注:本文章为原创文章,版权归文章作者与超越PHP网站所有,未经本站同意,禁止任何商业转载。非盈利网站及个人网站转载请注明出处,谢谢合作!