optionally return class name from gettype
| From: | Timothy Younger | Date: | Fri, 29 Jul 2016 13:53:57 +0000 |
| Subject: | optionally return class name from gettype | ||
| Groups: | php.internals | ||
| Request: | Send a blank email to internals+get-94743@lists.php.net to get a copy of this message | ||
hi Internals Team,
i find myself writing this line frequently in projects:
is_object($value) ? get_class($value) : gettype($value);
it seems i'm not the only one:
https://github.com/search?l=php&q=get_class+gettype&ref=searchresults&type=Code&utf8=%E2%9C%93
although it is small, it amounts to a lot of duplicated logic in need of
testing. i'm looking to write that line, and its tests, one last time.
i think it's a bit trivial to warrant a standalone lib, but might already
exist or fit in some helper package.
instead, what do you think about an optional param for the gettype method
to provide this functionality? here's the new (hopefully
backwards-compatible) behavior:
var_dump(gettype(new \stdClass(), true));
string(8) "stdClass"
var_dump(gettype(new \stdClass()));
string(6) "object"
here's my commit, proposed for php/php-src:
https://github.com/abacaphiliac/php-src/commit/f4e2fae9145cec5af94f197acf71dae02cd61b08
and my commit's build on travis:
https://travis-ci.org/abacaphiliac/php-src/builds/148218512
any interest? if gettype isn't the right home for this logic, then perhaps
someplace else?
thank you.
Tim Younger