PHP是否支持运算符重载? [英] Does php support operator overloading?
问题描述
php是否支持运算符重载?
Does php support operator overloading?
我正在尝试创建一个带有日期的类,并且希望将其与另一个对象进行比较,而不必使用方法或属性.
I'm trying to create a class that takes in a date and I'd like to compare it with another object without having to use methods or properties.
这就是我想要做的:
$obj1 = new myClass('2016-08-15');
$obj2 = new myClass('2016-02-06');
if ($obj1 > $obj2){
...
}
我知道如何进行日期比较,我只需要知道如何使运算符超载>,<和==.
I know how to do the date comparison, all I need to know is how to overload the operators >, < and ==.
谢谢.
推荐答案
PHP不支持运算符重载.
PHP doesn't support operator overloading.
有一个古老的扩展名,允许以类似于python的方式执行此操作.您可以在此处找到它.
There is an ancient extension which allowed to do this in a manner similar to python. You can find it here.
话虽这么说,当今最好的选择是只使用常规方法进行比较.
That being said, your best bet nowadays is to just use regular methods for comparison.
这篇关于PHP是否支持运算符重载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!