php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #9331 No operator overloading available
Submitted: 2001-02-19 02:07 UTC Modified: 2007-11-27 11:12 UTC
Votes:37
Avg. Score:4.5 ± 0.9
Reproduced:29 of 29 (100.0%)
Same Version:5 (17.2%)
Same OS:4 (13.8%)
From: jgsmith at tamu dot edu Assigned:
Status: Not a bug Package: Feature/Change Request
PHP Version: 4.0.4pl1 OS: Irix 6, Solaris 7, OpenBSD 2.8
Private report: No CVE-ID: None
 [2001-02-19 02:07 UTC] jgsmith at tamu dot edu
It would be nice to have operator overloading available for 
classes.  Something along the lines of Perl and C++.  I'm 
sure there are other OO languages that provide similar 
functionality.  This would go a long way to allowing the 
creation of object classes that are similar to those built 
into the language.

At least one class I'm wanting to create that could use this 
is an ordered set -- use +,-,*,/ for set union, asymmetric 
difference, intersection, and difference, for example.

Perhaps borrowing the C++ syntax wouldn't be a great burden. 

function operator+(objectClass,objectClass) { ... }

Unfortunately, this would require prototyping of class 
methods, but this is already implicately done for the 
operators (as far as the coder is concerned, even if not in 
fact).

Perhaps one way around the need for an explicit prototype is 
to consider the first argument of the binary operation (the 
left operand) as the governing class for finding the method.  
Then $A+$B == $A->operator+($B) and the function declaration 
is reduced to 

class A {
  function operator+($B) { ... }
}

I think this is important enough to the development of the 
language, and I didn't find mention of it in any of the 
documentation or archives I searched, that I am submitting 
it as a bug report for t

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-02-20 01:00 UTC] andre@php.net
I hardly believe this will be implemented anytime soon or
accepted at all,
suspending (for the records)
 [2007-04-21 16:49 UTC] dionyziz at deviantart dot com
I think there are many other people who would like to see this feature implemented. Is there any chance it could be made available in PHP6?
 [2007-08-11 07:58 UTC] nibujoshua at gmail dot com
it is very bad, that there is no operator overloding capability for PHP
 [2007-11-27 10:51 UTC] C01t dot te at gmail dot com
Operator overloading will open a new chapter in PHP's objects usage.

And I think that perhaps it could be done through interfaces. May be included in SPL. 

For example

interface MathOperators
{
   function operatorAdd($object);
   function operatorSub($object);
   function operatorMul($object);
   function operatorDiv($object);
   function operatorInc($object);
   function operatorDec($object);
}

One who needs these functionality just implements this interface (or interfaces for each operation) and here it is, and operator overloading :-)
 [2007-11-27 11:12 UTC] johannes@php.net
There's an operator overloading extension, see pecl.php.net -> search for "operator" but such a feature won't go in PHP's core.
 [2011-07-28 15:16 UTC] f3n6s at objectmail dot com
Saying "such a feature won't go in PHP's core."  Is tantamount to saying "There shall never be operator overloading in PHP"  since, if it's not in the core, nobody can use it practically!  Fine. Don't put it in the core.  Put it on the back of a flying monkey. I don't care.  But /enable/ it by default so people can use it.

Second- mysterious eyebrow raising followed by "well i dunno.. thre are many problems, what if we want to vardump an array operator overloard object" and whatnot is completely backwards.

You have destroyed and forgotten and thrown away everything that was once great about PHP.  If such eyebrow raising had occurred when you added __get and __set, and heebee-jeebee about "proper object oriented design", then those would never have made it in.  Same for a milion other features that made PHP unique and special, capable of things other languages just /couldn't do/.

Third, it's been written!  575 lines of clean, simple, straightforward code sitting there on PECL.  Dare-I-say, even /trivial/!

And as for the comments of "oh, well, there are side effects we would have to work out..."  If I don't want the side effects then I won't use the construct.   __toString has /side/effects!  

As I said, there were things; humble things; that once made PHP great.  But around the time that Zend and SPL and the rest of that junk came out, those things became less important, and PHP has incrementally become just like every other langauge out there.  

In response to this little gem form the "php manual user note system":

  - Your disdain for PHP and/or its maintainers. Go learn FORTRAN instead. 

Well heck, why don't we all go use JAVA if we wanna do "proper OOP".  

I'd much rather do cool things in half the time.

/rant
 [2012-07-03 15:01 UTC] ivebeenlinuxed at googlemail dot com
I agree, this is a significant feature missing from PHP and this shrugging it off 
is really just not helpful. I cannot implement function overloads into a PHP 
script for a production environment if it is not included in the PHP core or 
default deployment packages.
 [2013-08-15 14:59 UTC] themastersleader at hotmail dot com
I so agree with "f3n6s at objectmail dot com"

I had many cases that i needed the operator overloading.
Saying it will never get into the core.

It's even stranger that the DateTime class is "hacked" to overload the equality 
operators.
 [2014-07-15 10:14 UTC] php-operator-overloading at mandark dot fr
+1
 [2015-10-20 11:08 UTC] a at ustimen dot co
I propose to add this as a module which is disabled by default.

Many other standard enabled modules are useless out-of-the box, so we should give to average PHP developers a choice to use or not use this feature.
 [2016-01-29 00:45 UTC] ptheofan at gmail dot com
+1
Besides the obvious advantages (and horrorful disadvantages of bad practices), it could benefit the optimization/performance of the code execution.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 20:01:29 2024 UTC