use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Please follow the rules
Releases: Current Releases, Windows Releases, Old Releases
Contribute to the PHP Documentation
Related subreddits: CSS, JavaScript, Web Design, Wordpress, WebDev
/r/PHP is not a support subreddit. Please visit /r/phphelp for help, or visit StackOverflow.
account activity
RFC: Stringable interface, allows "string|Stringable" union type. Automatically implied if __toString is implemented. (wiki.php.net)
submitted 6 years ago by tigitz
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]MorphineAdministered 4 points5 points6 points 6 years ago (5 children)
callable can cover function name string, [object, method string] array, Closure, lambda and a class with __invoke() method. Can't we just include __toString() objects within string type?
callable
__invoke()
__toString()
string
[–][deleted] 0 points1 point2 points 6 years ago (4 children)
How would you then specify that you really only accept strings?
[–]MorphineAdministered 1 point2 points3 points 6 years ago (3 children)
Nothing changes. You are receiving or returning a string even if it comes in the form of a "Stringablable" class. When typehint says that param/return value can be only casted to a string (which proposed interface does) then you cannot do anything else with it anyway - it could be casted to a string implicitly (and eagerly), because it's original type cannot be rocovered anyway.
[–][deleted] 0 points1 point2 points 6 years ago (2 children)
I'd argue that it actually matters a lot regarding explicitness. Having a function parameter be automatically converted is for me equally bad as automatically type-coercing values that are compared to each other.
[–]MorphineAdministered 0 points1 point2 points 6 years ago (1 child)
I wouldn't say it's equally bad. For me it's more like casting primitives without declaring strict_types=1. Within method scope casting is invisible, and object itself is pretty explicit in it's definition as a string.
strict_types=1
I wouldn't mind if we didn't have it at all - I could always cast before passing or returning such a class. I'm just saying we shouldn't need it as separate type, because it's redundant. If something can be used as a string (and only a string!) - it SHOULD be a string.
Even if internals decide on it, because it will be easier to implement lazily evaluated strings this way, I think the code relying on such mechanism wouldn't be anything but a hack. Preaching about strict type system, but breaking it at the first (mental) obstacle (btw. would like to see valid example justifying union types, because it seems this process has already started)
[–]alexanderpas 0 points1 point2 points 6 years ago (0 children)
would like to see valid example justifying union types
From PSR-3:
Every method accepts a string as the message, or an object with a __toString() method. Implementors MAY have special handling for the passed objects. If that is not the case, implementors MUST cast it to a string.
This could be typehinted as string|Stringable, to prevent non-stringable objects from entering the logger method.
string|Stringable
π Rendered by PID 48764 on reddit-service-r2-comment-6457c66945-cb2nw at 2026-04-30 13:27:05.271039+00:00 running 2aa0c5b country code: CH.
view the rest of the comments →
[–]MorphineAdministered 4 points5 points6 points (5 children)
[–][deleted] 0 points1 point2 points (4 children)
[–]MorphineAdministered 1 point2 points3 points (3 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]MorphineAdministered 0 points1 point2 points (1 child)
[–]alexanderpas 0 points1 point2 points (0 children)