> On Thu, 3 Feb 2005, Sebastian Bergmann wrote:
>
> > Derick Rethans wrote:
> > > This adds operator overloading to user classes?
> >
> >  Yes, have a look at Johannes' Complex example [1].
>
> Okay, mega Yuck then. Although it looks cool, I consider it as a bad
> practise. It confuses the hell out of people that they can add two
> objects.

That's only confusing if you don't know that operator overloading is
possible. In C++, it's perfectly natural to be able to e.g. add values of
user-defined types. It also enables generic code, such as:

function some_calculation($num1, $num2)
{
   $num1+=1;
   $num2+=num1;
   ...
}

This would then work for any object (built-in type or user-defined type
(class)) having operator+= defined.

Why this would be confusing is beyond me... Could you enlighten me?

Specifically, do you find the following:

$result=$c1.multiply($c2).divide($c1.add($c2));

less "confusing" than:

$result=($c1 * $c2) / ($c1 + $c2);

?

>Use C++/Java if you want this...

Why would it be ok there, but not in PHP? It also exists in other scripting
languages, such as Python and Perl.

BTW, as another poster pointed out, it doesn't exist in Java, either, except
for the "+" exception for strings. I think omitting that is a case of
"throwing the baby out with the bath water". Yes, used incorrectly, operator
overloading can be confusing, but that's also the case for more or less any
language feature, and we don't ban them. It's no less confusing having the
member function "add()" perform subtraction, as it is to let the "+" do
that.

Regards,

Terje

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to