POSTS
Funny __toString() behavior
So here's another behavior in PHP that's not quite what I expected, but not all that surprising.
<?php
class SomeClass {
public function __toString() {
return "hello!";
}
}
class SomeOtherClass {
public function __toString() {
return new SomeClass();
}
}
echo new SomeOtherClass();
?>
Now, doesn't it seem that the echo should carry through to SomeClass::__toString()
? You'd think; but you get a fatal error instead. It the "fix" isn't difficult:
class SomeOtherClass {
public function __toString() {
return (string)new SomeClass();
}
And from a performance standpoint, it is better to require a string be returned than try to guess. Seemed odd, though, given PHP's loose typing.