|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2010-02-08 22:49 UTC] sylvain at abstraction dot fr
Description: ------------ I would like to know what is the current PHP team position about operator overloading in PHP for classes. Operator overloading has been stated Bogus in http://bugs.php.net/bug.php?id=9331 but it was more than two years ago. I think it would be nice to have a discussion about that topic. If you recently had one I would be interested if you could indicate me where I can find it. Best Regards. PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Dec 04 02:00:02 2025 UTC |
Well it is implemented, as you can see when following the link, I myself once created a patch to make it a bit nicer in syntax but it will never be implemented (as far as never goes) And well the issue, in the most simple form, is something like this: function foo() { return bar() + baz(); } In today's world during are view you can say "this will return a numeric value"with operator overloading you can say "this will return something or maybe fail" Becomes especially "funny" with return bar() + 4; Sure there's convention and comments and things but that won't help and you can always do return bar()->add(baz()); or return someclass::add(bar(), baz()); which makes the intention clear. Yes $a + $b + $c + $d might be bit nicer to read than the long form - but only as long as you know what types you have ...Well it is maybe implemented but it's beta and has not been updated for over 3 years. It is not usable in a production environment. Regarding your point that it is not readable, objects implementing Iterator can be used with the foreach statement (which is an overloading) and for objects with multiples propertiesn you don't know what is done unless you read the code. So, I don't see why we could not have overator overloading already having foreach overloading. Things can get "funny" with overloading, yes, but that's not a reason to not implement it. You can't state you won't allow something because some people will not use it correctly, in that case, you allow nothing and you don't implement a dynamically weak typed language. It is up to the developer to secure its implementation. You must have a little faith in him. Here an exemple of what I have in mind based on the Iterator model : interface OperatorOverloading { public function __add($object); public function __substract($object); public function __multiply($object); public function __divide($object); public function __increment(); public function __decrement(); } class foo implements OperatorOverloading { private $anynumber = 0; public function __add($object) { if (!$object instanceof __CLASS__) { throw new Exception('Come on !!!'); } $return = new foo(); $return->anynumber = $this->anynumber + $object- >anynumber; return $return; } public function __increment() { $this->anynumber++; } public function __decrement() { $this->anynumber--; } public function __substract($object) { throw new Exception('not implemented'); } public function __divide($object) { throw new Exception('not implemented'); } public function __multiply($object) { throw new Exception('not implemented'); } } Regards.