HomePhabricator

Introduce "phutil_string_cast()" to work around interesting choices in "…

Authored by epriestley on Feb 11 2019, 6:25 PM.

Description

Introduce "phutil_string_cast()" to work around interesting choices in "(string)" behavior

Summary:
Ref T13250. Two issues here:

  • When you (string)$x, if __toString() raises an exception: you get a fatal error, not an exception.
  • When you (string)$x, if $x is an array, you get the string "Array". This is almost certainly never desirable.

We can't fix this in the general case (and sometime string casts will still happen implicitly and there's nothing we can do about it), but we can implement a better version of (string) at the application layer for cases where we're doing an explicit cast.

(I believe there's a legitimate-ish reason that __toString() can't always raise exceptions, like if you call strlen($obj) and it needs to be implicitly cast inside parameter construction for the call or something? So this behavior isn't completely pointless, probably, just not-so-great.)

Test Plan:
Replaced the culprit (string) in T13250 with phutil_string_cast(), with exception improvements applied but no HTTP query string fixes.

  • Before:

  • After:

Reviewers: amckinley

Reviewed By: amckinley

Maniphest Tasks: T13250

Differential Revision: https://secure.phabricator.com/D20139