ID:               50969
 User updated by:  sylvain at abstraction dot fr
 Reported By:      sylvain at abstraction dot fr
 Status:           Bogus
 Bug Type:         Feature/Change Request
 Operating System: all
 PHP Version:      5.3.2RC1
 New Comment:

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.


Previous Comments:
------------------------------------------------------------------------

[2010-02-08 23:54:47] [email protected]

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 ...

------------------------------------------------------------------------

[2010-02-08 23:15:17] sylvain at abstraction dot fr

I'm not particularly aware of operator overloading in other language
but I don't see why it would be less readable in PHP than in any other
language as the only way to know what is happening is to read the code
of the method overloading the operator.

Anyhow, do I have to take as official PHP Team position that you won't
implement it ?

------------------------------------------------------------------------

[2010-02-08 22:59:25] [email protected]

Operator overloading in a dynamically weak typed language is bad as
reading code gives no information what's happening. If you really want
it go to http://pecl.php.net/operator maybe that still works.

------------------------------------------------------------------------

[2010-02-08 22:49:23] 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.



------------------------------------------------------------------------


-- 
Edit this bug report at http://bugs.php.net/?id=50969&edit=1

Reply via email to