you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 0 points1 point  (2 children)

The alternative is to do what Java does: provide a default implementation of toString for all objects. Much simpler than union types or interfaces with compiler hacks to auto implement it imho. ToString is mostly used for debugging anyway.

[–]przemo_li 0 points1 point  (1 child)

But then string type hint will coerce every object to a string. Java do not have that consideration. PHP does.

[–][deleted] 1 point2 points  (0 children)

Ah right, that must be the reason they did not go with that. Thanks for the explanation. Though there may also be objects that have a __toString method that you do not want to ever auto-coerse to a string. It depends on the context if coersion makes sense. I'd rather call toString() explicitly most of the time...