|
Posted by Jerry Stuckle on 06/22/07 01:15
james.gauth@googlemail.com wrote:
> 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?
>
No, PHP has been lousy at maintaining backward compatibility.
> 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.
>
Which version are you looking at? I don't see this in my 5.2.21
version. But it's not unusual for doc to lag the actual code changes, -
in PHP or any other product.
Irregardless, they do say they will use the __tostring() method, if
available.
Good programmers wouldn't leave something like this to the
compiler/interpreter; they'd define it themselves, anyway. Otherwise
you're just asking for bugs.
> 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'.
>
Gee, just like any other missing method. Again - EXACTLY which version
of the doc are you using?
> 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 gain a FALSE COMFORT that a string casting will work in some usable
manner.
> 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).
>
You gain consistency - because now you are defining the __tostring()
method in a manner which makes sense to your program (and your class).
Not something PHP has to guess at.
In case you don't get it - I'm all for this change. I think one of
their more serious mistakes was to implement to implicit string
conversion for user-defined objects in the first place. It just
encourages more sloppy programming.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Navigation:
[Reply to this message]
|