且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

是否可以在PHP中重载运算符?

更新时间:2023-11-14 14:14:16

如果您正在使用PHP5(应该如此),请查看

If you are using PHP5 (and you should be), take a look at the SPL ArrayObject classes. The documentation isn't too good, but I think if you extend ArrayObject, you'd have your "fake" array.

这是我的简单示例;恐怕我没有有价值的用例:

Here's my quick example; I'm afraid I don't have a valuable use case though:

class a extends ArrayObject {
    public function offsetSet($i, $v) {
        echo 'appending ' . $v;
        parent::offsetSet($i, $v);
    }
}

$a = new a;
$a[] = 1;