-
Notifications
You must be signed in to change notification settings - Fork 192
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RFC: Support for Options Array and PHP 8.0 Named Parameters #325
Comments
Please don't. All it does is add complications and complexity. Remember that the number of PHP installations that support named arguments (PHP 8.0+) will only ever increase from now on, and those that do not will only ever decrease. You'd be adding extra complexity to your code to support a user base that will only ever shrink, no matter what you do. Plus, if you're only dealing with 2-4 arguments, it's not a huge burden to just use positional arguments. We've used positional arguments for 25 years now just fine, thank you, and in practice most function/method calls will continue to do so for the foreseeable future. Telling people "call this method the same way that billions of lines of code have done for the last quarter century... or upgrade to PHP 8 if you really need to" is a very reasonable stance to take. |
@Crell thank you for weighing in! While the example only uses two options, this paradigm would be implemented in cases where there are more than 2-4. In #322, the options arrays have 5-7 arguments, and the |
😱 Unless that's a value object with lots of optional parameters, I'd consider that a code smell on its own that requires refactoring, not clumsy workarounds. If you must have an options-array-alike, convert it to an object in the first place. At which point you can put a real API on it, and potentially even shift the functionality to that object instead. |
As a user of these libraries (and the GCloud SDK), I am in favor of supporting Named Parameters for PHP 8 users. It would certainly be easier to understand than an |
This is now being discussed here: googleapis/google-cloud-php#5147 |
RFC for supporting both named parameters in PHP 8 and options arrays for previous versions of PHP
Given class
Foo
with two "options" in its constructor,string cacheKey
andint cacheLifetime
, this would give us the following behavior in PHP 8.0:And the following behavior in PHP 7.4 and below:
For this case, the class constructor would look like:
The following cases would throw exceptions:
It's not possible (AFAICT) to throw exceptions in the following non-standard scenarios:
This behavior is done by checking if
$options
is an array and callingfunc_num_args()
. Using a trait, we can guarantee standard behavior across all our classes:Pros
Cons
The text was updated successfully, but these errors were encountered: