rfc:replace_parse_url

This is an old revision of the document!


PHP RFC: Create URL Class

Introduction

This RFC came about for an attempt to resolve Bug #72811. In the attempt, discussion shifted from trying to patch the current implementation of parse_url() to more generally replacing the current one. The discussion then shifted to the inability to remove parse_url() due to BC issues. Ideas formed on creating an immutable class that will take a URL and parse it, exposing the pieces by getters.

The current implementation of parse_url() makes a bunch of exceptions to RFC 3986. I do not know if these are conscious exceptions, or, if parse_url() was never based off of following the RFC. After raising this RFC, I was alerted that the RFC, is itself, generally superseded by WHATWG spec on URLs. This is a more practical specification to how URLs exist in the real-world.

So, this RFC proposes creating two new classes, URLParser and URLBuilder. The former will be an immutable class, that is constructed with a URL to be parsed. There will be methods to access each piece of the URL, as well as a general getter, that will accept a string of flags that will return requested portions in an array. The complimentary to this will be URLBuilder, which will expose methods to set, or add, pieces to a URL, and a method to get the built value.

Proposal

<?php
 
class URL {
    public __construct(string $url, string|URL $base);
    public getScheme() : ?string;
    public getUsername() : ?string;
    public getPassword() : ?string;
    public getHostname() : ?string;
    public getPort() : ?int;
    public getPath() : ?string;
    public getQuery() : ?string;
    public getFragment() : ?string;
}

Backward Incompatible Changes

None

RFC Impact

To Existing Extensions

standard

Future Scope

Discussion brought forward the other half of this change being a URLBuilder class that is mutable. It would allow users to specify each portion of a URL without worrying about managing correct syntax.

Open Issues

  • Deprecate parse_url()? Try and push people into using the new URLParser class.
  • Should parse_url() have a sunset date of PHP8, or PHP9?

Proposed Voting Choices

Vote to replace parse_url() with an re2c parser, and require standard compliant URI formats. Requires 2/3

Implementation

After the project is implemented, this section should contain

  1. the version(s) it was merged to
  2. a link to the git commit(s)
  3. a link to the PHP manual entry for the feature

References

PR with working Implementation: https://github.com/php/php-src/pull/2079

rfc/replace_parse_url.1476132948.txt.gz · Last modified: 2017/09/22 13:28 (external edit)