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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/787692/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:50:23  来源:igfitidea点击:

Is it possible to overload operators in PHP?

phpoperator-overloading

提问by chadgh

Specifically, I would like to create an Array class and would like to overload the [] operator.

具体来说,我想创建一个 Array 类并想重载 [] 运算符。

采纳答案by cbeer

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

如果您使用的是 PHP5(您应该使用),请查看SPL ArrayObject类。文档不太好,但我认为如果您扩展 ArrayObject,您将拥有“假”数组。

EDIT: 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;

回答by panicz

Actually, the optimal solution is to implement the four methods of the ArrayAccess interface: http://php.net/manual/en/class.arrayaccess.php

实际上,最佳的解决方案是实现ArrayAccess接口的四个方法:http://php.net/manual/en/class.arrayaccess.php

If you would also like to use your object in the context of 'foreach', you'd have to implement the 'Iterator' interface: http://www.php.net/manual/en/class.iterator.php

如果您还想在“foreach”的上下文中使用您的对象,则必须实现“Iterator”接口:http: //www.php.net/manual/en/class.iterator.php

回答by grammar31

PHP's concept of overloading and operators (see Overloading, and Array Operators) is not like C++'s concept. I don't believe it is possible to overload operators such as +, -, [], etc.

PHP 的重载和运算符的概念(请参阅重载数组运算符)与 C++ 的概念不同。我认为不可能重载 +、-、[] 等运算符。

Possible Solutions

可能的解决方案

回答by Fabien Sa

For a simple and clean solution in PHP 5.0+, you need to implements the ArrayAccessinterfaceand override functions offsetGet, offsetSet, offsetExists and offsetUnset. You can now use the object like an array.

为了在 PHP 5.0+ 中获得简单干净的解决方案,您需要实现ArrayAccess接口和覆盖函数 offsetGet、offsetSet、offsetExists 和 offsetUnset。您现在可以像使用数组一样使用该对象。

Example:

例子:

<?php
class A implements ArrayAccess {
    private $data = array();

    public function offsetGet($offset) {
        return isset($this->data[$offset]) ? $this->data[$offset] : null;
    }

    public function offsetSet($offset, $value) {
        if ($offset === null) {
            $this->data[] = $value;
        } else {
            $this->data[$offset] = $value;
        }
    }

    public function offsetExists($offset) {
        return isset($this->data[$offset]);
    }

    public function offsetUnset($offset) {
        unset($this->data[$offset]);
    }
}

$obj = new A;
$obj[] = 'a';
$obj['k'] = 'b';
echo $obj[0], $obj['k']; // print "ab"

回答by Benson

It appears not to be a feature of the language, see this bug. However, it looks like there's a packagethat lets you do some sort of overloading.

它似乎不是该语言的功能,请参阅此错误。但是,看起来有一个可以让您进行某种重载。

回答by dirtside

Put simply, no; and I'd suggest that if you think you need C++-style overloading, you may need to rethink the solution to your problem. Or maybe consider not using PHP.

简单地说,不;我建议,如果您认为需要 C++ 风格的重载,您可能需要重新考虑问题的解决方案。或者考虑不使用 PHP。

To paraphrase Jamie Zawinski, "You have a problem and think, 'I know! I'll use operator overloading!' Now you have two problems."

套用 Jamie Zawinski 的话来说,“您遇到问题并想,'我知道!我将使用运算符重载!' 现在你有两个问题。”