Last active
February 7, 2020 16:30
-
-
Save asheliahut/8bc5f09bb9b42ebf1984e2465bc3b241 to your computer and use it in GitHub Desktop.
ObjectCheckFirst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // This is an object that can come from a few sources into a reusable resolve for urls for a graphql resolver | |
| // can also be an array from a different source. | |
| $value = (object) ['url' => 'https://example.com']; | |
| // As array | |
| $arrayValue = ['url' => 'https://example.com']; | |
| /** | |
| * The normal definition for the function is in the doc block. | |
| * @param $value | |
| * @param array $args | |
| * @param ResolveInfo $info | |
| * | |
| * @return string|null | |
| */ | |
| function resolve($value): ?string | |
| { | |
| //This is the only actual thing this function does | |
| return $value['url'] ?? $value->url ?? null; | |
| // This option will always work in both cases | |
| // return $value->url ?? $value['url'] ?? null | |
| } | |
| // This one fails | |
| var_dump(resolve($value)); | |
| // This one passes | |
| var_dump(resolve($arrayValue)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment