Operator overloading in PHP

PHP Web development July 8th, 2008 by Eran Galperin

Operator overloading is a programming language features that allows operators to act differently depending on the type of data they are operating on. Since OOP lets us create custom types (classes), there are plenty of opportunities to do useful and interesting code manipulations using operator overloading.

Current operator overloading in PHP is somewhat limited. Most of it is contained in class methods delimited by two underscores (__) which are called magical functions. Currently available are string casting and automatic serializing / unserializing (you can read more about those in the manual). A native PHP5 extension called SPL (Standard PHP Library) implements some array operator overloading to provide iterateable objects, which was a very welcome addition in PHP5.

A recent article I read shows some very imaginative techniques to implement mathematical operator overloading using a PECL extension called opreator. The author's implementations are very interesting, and he's pushing for a native overloading implementation to make for a much easier use of such techniques.

Apparently, operator overloading is a recurring subject on the PHP internals. Developers coming to PHP from languages that support operator overloading natively have a tendency to request it as a language feature in PHP, but it appears to be a complex and loaded issue. There is even an issue opened on the matter in the PHP issue tracker, describing the lack of operator overloading in PHP as a bug.

Another thing to consider is that PHP already does some minor operator overloading - for example the plus operator (+) can be used on arrays to perform array merging. If you used the plus operator on two objects, how would it know whether to cast it into arrays or into numbers? Those ambiguities only add to the implementation headaches for the internal PHP dev team.

If those issues could be resolved, it would be very nice to have native magical methods such as __toArray() and __toNumber() which would open up a plethora of possible uses.

Enter your email address to receive notification about new posts.

If you liked this article you should follow me on Twitter and/or share below:
  • http://usrportage.de Lars Strojny

    The issue with operator overloading is, that it is rarely used correctly. The only relevant usage examples are so called value objects. The classical “Money” object, e.g. So I think the benefit is relatively minimal and the potential for abuse is huge. Nevertheless, I would not mind a proper RFC and a patch.

  • http://usrportage.de Lars Strojny

    The issue with operator overloading is, that it is rarely used correctly. The only relevant usage examples are so called value objects. The classical “Money” object, e.g. So I think the benefit is relatively minimal and the potential for abuse is huge. Nevertheless, I would not mind a proper RFC and a patch.

  • http://www.techfounder.net Eran Galperin

    If it’s used correctly or not, that’s up to the developer. The same could be said of many other language features ;)

    I think some additional operator overloading features would allow us to create more natural API’s and to have convenience options which never hurts. I personally don’t see it as a priority, but if it could be implemented it would be a ‘nice-to-have’.

  • http://www.techfounder.net Eran Galperin

    If it’s used correctly or not, that’s up to the developer. The same could be said of many other language features ;)

    I think some additional operator overloading features would allow us to create more natural API’s and to have convenience options which never hurts. I personally don’t see it as a priority, but if it could be implemented it would be a ‘nice-to-have’.

  • http://ishouldbecoding.com Matthew Turland

    I’ve always wondered why a period was used for string concatenation in PHP instead of a plus sign as other dynamically typed languages like Python and JavaScript do, especially since the plus sign is used for array unions.

    The only other case I know of for operator overloading offhand is matrices as they are used in linear algebra, but that could just as easily be accomplished using methods.

    In short, operator overloading would only provide syntactic sugar where methods to accomplish the same purposes already exist. The same can be said for issues that seem to continue resurfacing such as named parameters or shorthand array notation.

  • http://ishouldbecoding.com Matthew Turland

    I’ve always wondered why a period was used for string concatenation in PHP instead of a plus sign as other dynamically typed languages like Python and JavaScript do, especially since the plus sign is used for array unions.

    The only other case I know of for operator overloading offhand is matrices as they are used in linear algebra, but that could just as easily be accomplished using methods.

    In short, operator overloading would only provide syntactic sugar where methods to accomplish the same purposes already exist. The same can be said for issues that seem to continue resurfacing such as named parameters or shorthand array notation.

  • http://www.techfounder.net Eran Galperin

    Actually there is some to be said for using a period to concatenate strings since it allows the plus operator to perform mathematical calculations on variables without worrying about their type. This distinction always annoyed me in Javascript since it forces you to constantly cast numerical strings into numbers so you could perform calculations with them. HTML inputs will always return a string value, so having this happen automatically in PHP is useful on the web.

    But I was actually referring more to array operator casting. Being able to perform array merging on two objects using the plus sign, and having their __toArray() overloading method kick in and return the a merged array would be very useful in a lot of applications.

  • http://www.techfounder.net Eran Galperin

    Actually there is some to be said for using a period to concatenate strings since it allows the plus operator to perform mathematical calculations on variables without worrying about their type. This distinction always annoyed me in Javascript since it forces you to constantly cast numerical strings into numbers so you could perform calculations with them. HTML inputs will always return a string value, so having this happen automatically in PHP is useful on the web.

    But I was actually referring more to array operator casting. Being able to perform array merging on two objects using the plus sign, and having their __toArray() overloading method kick in and return the a merged array would be very useful in a lot of applications.

  • http://www.tpoparts.com Noah

    Properly used, operator overloading allows us to develop clean library interfaces, with obvious functionality. I am a native PHP coder, with a C background (not C++). I was introduced to object orientation in PHP–not from other languages–and yet, I have desired this “sugar” for quite a while.

    I haven’t seen a technical reason why it shouldn’t be included, and the existence of code which cleanly implements it tends to refute any such argument in vitro.

  • http://www.tpoparts.com Noah

    Properly used, operator overloading allows us to develop clean library interfaces, with obvious functionality. I am a native PHP coder, with a C background (not C++). I was introduced to object orientation in PHP–not from other languages–and yet, I have desired this “sugar” for quite a while.

    I haven’t seen a technical reason why it shouldn’t be included, and the existence of code which cleanly implements it tends to refute any such argument in vitro.