guzzle / guzzle

Guzzle, an extensible PHP HTTP client

Home Page:https://docs.guzzlephp.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Support named arguments instead of config arrays.

luckydonald opened this issue · comments

Description
Add support for php 8.0's Named Arguments for what previously would be an $config array.

This has the advantage that there could be strict typing, as well as better IDE code hints.

Yes, calling with named arguments, that is a PHP 8+ feature, but not on the function (i.e. guzzle) side.
There it would be just optional parameters in the constructor, and thus it would be both backwards compatible with existing implementations, as well as compatible with PHP 7.

So in detail this would mean to add optional arguments for all the settable options. Those would default to null, and once it's set to something proper by the caller, it will be merged into the options array. Alternatively the options array could fill variables instead, but that would be a bigger change which would make this change too heavy, and if wanted I'd like to do separately.

Overall I think it makes sense to embrace that new language feature.


Example

Before:

use GuzzleHttp\Client;

$client = new Client([
    'base_uri' => 'http://httpbin.org',
    'timeout'  => 2.0,
]);

After:

$client = new Client(baseUri: 'http://httpbin.org', timeout: 2.0);

With the GuzzleHttp\Client changing like this:
Before:

public function __construct(array $config = []) {
    …

After:

public function __construct(array $config = [], string $baseUri = null, float timeout = null, …) {
    …

Additional context

Screenshot of some code completion:
Screenshot 2022-09-21 at 10 25 19


Should I prepare a PR for that?

commented

This issue has been automatically marked as stale because it has not had recent activity. It will be closed after 2 weeks if no further activity occurs. Thank you for your contributions.

Apologies for the delay here. No, I don't like named parameters, and many major codebases, including the Laravel Framework do not consider parameter names to be part of the BC policy, making them largely unusable in practice, outside of application code calling application code.