是否可以在PHP中重载运算符? [英] Is it possible to overload operators in PHP?
本文介绍了是否可以在PHP中重载运算符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
具体来说,我想创建一个Array类,并希望重载[]运算符.
Specifically, I would like to create an Array class and would like to overload the [] operator.
推荐答案
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;
这篇关于是否可以在PHP中重载运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文