Greg Beaver mailto:cellog@php.net wrote on Monday, June 05, 2006 11:06
PM:
You miss the point. The warning that is helpful in debugging, it has
nothing to do with designing things to break, it has to do with
unintentional bugs. If in 1 case of 1000 in a loop, there is
an object
instead of a string used as a key due to a bug (one example: this can
easily happen if you forget to check for a PEAR_Error on return from a
legacy PEAR class), a warning is printed. With some kind of magic
conversion, this warning will instead translate into a magic
conversion to string, causing unpredictable and very
difficult-to-track-down behavior.
Yes, that's all really good point! I'm -1 on this __toString() magical
behaviour, too. __toString should only be called in a real string
conversion as echoing and concatenating objects with strings etc.
Maybe someone mentioned that before, but did you look at Java? You can
use real objects as offset in "HashTable"s. In PHP it would look like
that:
class c {
public function __toString() {
return 'c';
}
}
$o1 = new c;
$o2 = clone $o1;
$ar = array();
$ar[$o1] = 'Object 1';
$ar[$o2] = 'Object 2';
With __toString, the first array index gets overwritten. This is some
magic behaviour I don't like cause it's not intuitive. With the "Java
behaviour", the reference id or something similar would be used and you
could easily access these keys with:
var_dump($ar[$o1], $ar[$o2]);
Just my 2 cents ;)
-soenke