Is it possible to overload operators in php for objects, like in C?
For example, $obj1 is a class object, $obj2 is a class object.
and to perform the operation $obj1+$obj2 as I ask.
If possible, please give an example.
Answer 1, authority 100%
Operator overloading similar to
C++does not exist in
And this is not a plus or a minus, it’s just some decision made during the design of the language, for more details you can read here.
However, there are exceptions to the rule – for example, you can achieve functionality that is similar in meaning to overloading when it comes to the
operator. Example here.
Update (from comments)
If you mean redefine standard functions(because in classes everything is decided by ordinary polymorphism), then there are crutches. The way I know is
Advanced PHP Debuggerwhich supports the method
runkitwhich has method
Unfortunately, I have no idea how the second one works, but the first one does the rewriting of the symbols of the replaced functions. An analogy can be drawn, for example, using custom allocators in