Req #43638 [Opn->Wfx]: Infix Notation for Functions
| From: | requinix@php.net | Date: | Sat, 05 May 2018 19:18:18 +0000 |
| Subject: | Req #43638 [Opn->Wfx]: Infix Notation for Functions | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-215082@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=43638&edit=1
ID: 43638
Updated by: requinix@php.net
Reported by: schufre at cs dot tu-berlin dot de
Summary: Infix Notation for Functions
-Status: Open
+Status: Wont fix
Type: Feature/Change Request
-Package: Feature/Change Request
+Package: *General Issues
PHP Version: 5.2.5
Block user comment: N
Private report: N
New Comment:
Regarding functions as operators: use the RFC process https://wiki.php.net/rfc/howto
Regarding operator overloading: see https://wiki.php.net/rfc/operator_functions
Closing wontfix due to age. We can reopen if there's renewed interest.
Previous Comments:
------------------------------------------------------------------------
[2014-12-12 15:14:44] tom at r dot je
I'd also like to do this. It makes more sense in the world of OOP than procedural as we need
some type recognition to understand which implementation of the method to call:
A better syntax would be
class Foo {
public infix +($second) {
// do some operation with $this + $second
return $result;
}
}
$foo = new Foo();
$bar = new Bar();
$x = $foo + $bar;
the problem with this approach is that PHP does not support method overloading meaning it's
very difficult to clearly understand what's happening.
$foo + $bar in this instance should call Foo::+ with the first argument as the $bar instance.
However, this introduces a problem:
$bar + $foo
this would call Bar::+ with the argument $foo, potentially meaning that:
$foo + $bar
gives a different result to
$bar + $foo
which is at best confusing.
The best way of achieving this would be to enforce type-checking insofar as ($foo instanceof $bar)
or ($bar instanceof $foo) *must* evaluate to true before calling the infix method. This gives a
little sanity check because mostly it will call the same + implementation.
The only caveat here is if $bar is an instance of a class that extends from Foo and also contains a
+ method, the logical course of action, regardless of which side of the operator it's on, is to
call the method in the parent class rather than the subclass
class Foo {
public infix +(Foo $foo) {
return $result;
}
}
class Bar extends Foo {
public infix +(Bar $bar) {
//do something else
}
}
$foo = new Foo;
$bar = new Bar;
$foo + $bar;
Here, calling bar::+ may break because the Bar::+ implementation may use properties/methods only
available to the Bar class. The only sensible way of handling this is calling the Foo::+ method.
When calling + on two objects of the same type, it makes sense e.g. $bar + $bar it makes sense to
use the bar::+ implementation.
------------------------------------------------------------------------
[2007-12-19 15:41:58] schufre at cs dot tu-berlin dot de
Description:
------------
I think it would be a great feature if you'd be able to define functions in a way you can use
them in infix-notation lateron.
e.g. infix $x operand $b
{
// do stuff
}
just like $a + $b and so on.
if this is already implemented, please inform me, because I couldn'nt find it anywhere in the
documentation.
thanks in advance
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=43638&edit=1