|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Oct 24 11:00:02 2025 UTC |
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 :-)