php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #43638 Infix Notation for Functions
Submitted: 2007-12-19 15:41 UTC Modified: 2018-05-05 19:18 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: schufre at cs dot tu-berlin dot de Assigned:
Status: Wont fix Package: *General Issues
PHP Version: 5.2.5 OS:
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2007-12-19 15:41 UTC] 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


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-12-12 15:14 UTC] 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.
 [2018-05-05 19:18 UTC] requinix@php.net
-Status: Open +Status: Wont fix -Package: Feature/Change Request +Package: *General Issues
 [2018-05-05 19:18 UTC] requinix@php.net
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.
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Wed Nov 25 02:01:23 2020 UTC