Page MenuHomePhorge

function phutil_string_cast($value)
libphutil Technical Documentation (Core Utilities)

Perform a "(string)" cast without disabling standard exception behavior.

When PHP invokes "__toString()" automatically, it fatals if the method raises an exception. In older versions of PHP (until PHP 7.1), this fatal is fairly opaque and does not give you any information about the exception itself, although newer versions of PHP at least include the exception message.

This is documented on the "__toString()" manual page:

Warning
You cannot throw an exception from within a __toString() method. Doing
so will result in a fatal error.

However, this only applies to implicit invocation by the language runtime. Application code can safely call __toString() directly without any effect on exception handling behavior. Very cool.

We also reject arrays. PHP casts them to the string "Array". This behavior is, charitably, evil.

Parameters
wild$valueAny value which aspires to be represented as a string.
Return
stringString representation of the provided value.