Is it possible to overload operators in php for objects like in si?
For example, $ obj1 is an object class, $ obj2 is an object class.
and so that the operation $ obj1 + $ obj2 is performed as I specify.
If possible, please give an example.

1 answer 1

Operator overloading, similar to overloading in C++ missing in php .

And this is not a plus or a minus, it’s just some decision taken during the design of the language, you can read more here.


Nevertheless, there are exceptions to the rules - for example, it is possible to achieve functionality that is similar in its meaning to overload, if we are talking about the operator [] . An example is here.

Update (from comments)


If you mean to override the standard functions (because in classes everything is solved by ordinary polymorphism) , then crutches exist. Of the methods I know of is the Advanced PHP Debugger , which supports the override_function method and the runkit , which has the runkit_function_redefine. method runkit_function_redefine.

I, unfortunately, can’t imagine how the second one works, but the first one does the re-substitution of the characters of the functions being replaced. An analogy can be made, for example, using our own allocators in C++ projects.

  • Is it possible to redefine the functions? - iproger
  • one
    @mctrane Answered you an update. - Costantino Rupert