|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Dec 03 12:00:01 2025 UTC |
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.