Re: [RFC] [Discussion] Add WHATWG compliant URL parsing API
| From: | Tim Düsterhus | Date: | Fri, 29 Nov 2024 12:28:20 +0000 |
| Subject: | Re: [RFC] [Discussion] Add WHATWG compliant URL parsing API | ||
| References: | 1 2 3 4 5 6 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-126081@lists.php.net to get a copy of this message | ||
Hi
Am 2024-11-24 21:40, schrieb Máté Kocsis:
I disagree with this change and believe that with the current capabilities of PHP the out-parameter is the correct API design choice, because then the “failure” case would be returning a falsy value, which IMO is pretty idiomatic PHP:It took me a while to realize that, I think, the fromWhatWg() method is using an in/out parameter for error handling. That is an insta-no on my part. in/out reference parameters make sense in C, maybe C++, and basically nowhere else. I view them as a code smell everywhere they're used in PHP. Better alternatives include exceptions or union returns.Yes, originally the RFC used a reference parameter to return the error during parsing. I knew it was controversial, but that's what was a consistent choice with other internal functions/methods. After your feedback, I changed this behavior to an union type return type: public static function parse(string $uri, ?string $baseUrl = null): static|array {} So that in case of failure, an array of Uri\WhatWgError objects are returned. This practice is not really idiomatic with PHP, so personally I'm not sure I like it, but neither did I particularly like passing a parameter by reference...
if (($uri = WhatWgUri::parse($someUri, errors: $errors)) !== null) {
printf("Your URI '%s' is valid. Here it is: %s", $someUri, $uri);
} else {
printf("Your URI '%s' is invalid, there were %d errors.\n", $someUri, $errors);
}
It would also unify the API between Rfc3986Uri and WhatWgUri.
Best regards
Tim Düsterhus