On 2024-06-28 23:06, Máté Kocsis wrote:

Hi Everyone,

I've been working on a new RFC for a while now, and time has come to present it to a wider audience.

Last year, I learnt that PHP doesn't have built-in support for parsing URLs according to any well established standards (RFC 1738 or the WHATWG URL living standard), since the parse_url() function is optimized for performance instead of correctness.

In order to improve compatibility with external tools consuming URLs (like browsers), my new RFC would add a WHATWG compliant URL parser functionality to the standard library. The API itself is not final by any means, the RFC only represents how I imagined it first.

You can find the RFC at the following link: https://wiki.php.net/rfc/url_parsing_api

Regards,
Máté

Hey,

That's great that you've made the Url class readonly. Immutability is realiable. And I fully agree that a better parser is needed.

I agree with the otters that

- the enum might be fine without the backing, if it's needed at all
- I'm not convinced a separate UrlParser is needed, Url::someFactory($str) should be enough - getters seem unnecessary, they should only be added if you can be sure they are going to be used for compatibility with PSR-7 - treating $query as a single string is clumsy, having some kind of bag or at least an array to represent it would be cooler and easier to build and manipulate

I wanted to add that it might be more useful to make all the Url constructor arguments optional. Either nullable or with reasonable defaults. So you could `$url = new Url(path: 'robots.txt'); foreach ($domains as $d) $r[] = file_get_contents($url->withHost($d))` and stuff like that.

Similar modifiers would be very useful for the query stuff, e.g. `$u = Url::current(); return $u->withQueryParam('page', $u->queryParam->page + 1);`.

Sure, all of that can be done in the userland as long as you drop `final` :)

BR,
Juris

Reply via email to