|
Posted by james.gauth on 06/21/07 16:02
I agree with Gosha Bine, PHP prides itself with maintaining backward
compatibility (so much so that there's an unbelievable amount of cruft
in the language) yet they choose to break backward compatibility with
something like this? Why not allow a way to extend the base object
prototype to reproduce the old behaviour (or a more descriptive
behaviour)? Or why not, if a __toString() method isn't found, print
'Object' instead of throwing an exception?
Even the manual still states:
Objects are always converted to the string "Object". If you would like
to print out the member variable values of an object for debugging
reasons, read the paragraphs below. If you would like to find out the
class name of which an object is an instance of, use get_class(). As
of PHP 5, __toString() method is used if applicable.
No warning is given that if a __toString() method isn't defined it'll
throw an exception and handily fatal for you (since PHP 5.2). They
even state that a __toString() method is used 'if applicable'. I'm
guessing 'if applicable' in this case actually means 'without question
and will cause a fatal error in your scripts if you don't define it
when casting an object to a string'.
You *do* gain something from an implicit conversion to 'Object', you
gain the sure knowledge that string type-casting will work in *all*
cases regardless of variable type. You do not have to check what the
type of a variable is every time you would like to use it in a string
capacity. It's as if PHP is desperately trying to not be a loosely
typed language any more. The half-baked type-hinting feature is a
great example of this.
You lose far more from having your language behave inconsistently
between releases than you gain from forcing mandatory redundant coding
constructs. No mention is made in the change log with regards to what
is a reasonably intrinsic change to the language (type-casting should
be set in stone, or people should at least be made aware of it if it's
changed).
[Back to original message]
|