Re: Object Casting - An Alternative to Type Hinting

From: Date: Mon, 27 Feb 2012 02:45:45 +0000
Subject: Re: Object Casting - An Alternative to Type Hinting
References: 1 2 3 4  Groups: php.internals 
Request: Send a blank email to internals+get-58112@lists.php.net to get a copy of this message
> Or operator-overlading to the rescue? :-) Not quite. Especially because with operator overloading done at this level (how it would be implemented in PHP) it's almost impossible to make it consistent: class string { public function overload+($mixed) { return $this->value + $mixed; } } class Integer { public function overload+($mixed) { return $this->value + $mixed; } } $int = new Integer(5); $string = new String("5"); echo $int + $string; // 10 echo $string + $int; // 55 While I like the concept of overloading, I don't really think it's solvable in a consistent enough manner that it would work here. On Sun, Feb 26, 2012 at 11:12 AM, Stefan Neufeind <neufeind@php.net> wrote: > On 02/26/2012 04:48 PM, Anthony Ferrara wrote: >>> I have to say, it doesn't get work, thinking this: >>> >>> $mixed1 = new Interger(2); >>> $mixed2 = new Interge(3); >>> $guess_what_type_is = $mixed1 + $mixed2; >>> >>> thanks >> >> That one is actually pretty straight forward.  Since + is >> a numeric >> operation (with the one exception of array + array), it would call >> castTo('numeric') on both.  Then, the normal type rules would take >> over.  So if it returned an int, the result would be int(5).  If it >> returned a float, it would be float(5)... > > Or operator-overlading to the rescue? :-) > > > (Okay, I know that idea has been burried a long time ago - but might > come in hand here as well. > http://bugs.php.net/bug.php?id=9331 > http://pecl.php.net/package/operator > ) > > > Regards, >  Stefan > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php >

« previous php.internals (#58112) next »