Re: PHP 5.1

From: Date: Thu, 03 Feb 2005 16:23:30 +0000
Subject: Re: PHP 5.1
References: 1 2 3 4 5 6  Groups: php.internals 
Request: Send a blank email to internals+get-14570@lists.php.net to get a copy of this message
> Derick Rethans wrote: > > 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. Use C++/Java if you want this... > > I couldn't agree more. I like PHP not because it is the most compact or > flexible of all languages but because the syntax is quite simple. > > Try explaining someone who just learned PHP and doesn't know your code > where to find the implementation of > $c = $a + $b; > as opposed to > $c= $a->add($b); > or > $c = Complex::add($a, $b); If $a is an object of a class, then they would both be in the class definition. One is called "add", and the other is called "operator+". What's the problem with that? > Another little thought how confusion can easily happen with operator > overloading: $b = $a + 42; works but $b = 42 + $a; doesn't, i.e. the > overloaded addition isn't commutative. It would if operator overloading was allowed on free functions (as it is in C++). E..g: function operator+(complex $a, complex $b) { return new complex($a.real + $b.real, $a.imag + $b.imag); } The binary operators are recommended to to free functions, for this reason, in C++. Regards, Terje

« previous php.internals (#14570) next »