Argument with default value

php.internals

David Rodrigues

6 years ago
Hello! I would like to propose the idea of having a keyword that can be used to use the default value of a parameter, without necessarily having to know it. Since PHP is not a strong typing language, the default values they use are quite mixed, and it is difficult to predict each without knowing the documentation (if you have documented it) or using IDE support. public function __construct($data = []); public function __construct($data = null); I always prefer to use null as the default value to optional parameter, and then define the value transformation in case it is null. But a lot of third-party code defines how they want the default value. String: null or '' Array: null or [] Integer: null or 0 Boolean: null, true or false In addition to other values that can actually be used as defaults in a more specific way. public function test(int $pi = M_PI, int $a = null, int $b = 0) My suggestion would be to use the keyword "default" in the context of the argument. To avoid confusion, another keyword could be created, but I don't see why they confuse "default" as an argument and "default" for switch. test(default, 2) === test(M_PI, 2, 0) Atenciosamente, David Rodrigues

Christoph Becker

6 years ago
On 14.03.2020 at 16:27, David Rodrigues wrote:
> I would like to propose the idea of having a keyword that can be used to > use the default value of a parameter, without necessarily having to know > it. Since PHP is not a strong typing language, the default values they use > are quite mixed, and it is difficult to predict each without knowing the > documentation (if you have documented it) or using IDE support. > > public function __construct($data = []); > public function __construct($data = null); > > I always prefer to use null as the default value to optional parameter, and > then define the value transformation in case it is null. But a lot of > third-party code defines how they want the default value. > > String: null or '' > Array: null or [] > Integer: null or 0 > Boolean: null, true or false > > In addition to other values that can actually be used as defaults in a more > specific way. > > public function test(int $pi = M_PI, int $a = null, int $b = 0) > > My suggestion would be to use the keyword "default" in the context of the > argument. To avoid confusion, another keyword could be created, but I don't > see why they confuse "default" as an argument and "default" for switch. > > test(default, 2) === test(M_PI, 2, 0)
FWIW, this has been proposed as RFC[1] quite a while ago, but has been declined. [1] <https://wiki.php.net/rfc/skipparams>
-- Christoph M. Becker

Craig Duncan (PHP)

6 years ago
> > On Sat, 14 Mar 2020 at 15:53, Christoph M. Becker <cmbecker69@gmx.de> > wrote: > >> On 14.03.2020 at 16:27, David Rodrigues wrote: >> >> > I would like to propose the idea of having a keyword that can be used to >> > use the default value of a parameter, without necessarily having to know >> > it. >> >> FWIW, this has been proposed as RFC[1] quite a while ago, but has been >> declined. >> >> [1] <https://wiki.php.net/rfc/skipparams> >> >>
It was also briefly discussed last year: https://externals.io/message/105123

Dan Ackroyd

6 years ago
On Sat, 14 Mar 2020 at 15:27, David Rodrigues <david.proweb@gmail.com> wrote:
> > I would like to propose the idea of having a keyword that can be used to > use the default value of a parameter,
As it's arisen on the list again, I've written a note detailing my view of the previous discussion: https://github.com/Danack/RfcCodex/blob/master/explicit_defaults.md cheers Dan Ack

Stas Malyshev

6 years ago
Hi!
> As it's arisen on the list again, I've written a note detailing my > view of the previous discussion: > > https://github.com/Danack/RfcCodex/blob/master/explicit_defaults.md
As the author of that RFC, I am not particularly sure how it can be "improved", as main argument from what I understood has been pretty much "we should do named arguments instead" and as we can all see that isn't happening either. And I don't have time or energy to go through another fight on this. But if somebody wants to, I could possibly help with technical details if necessary (whatever I remember from 5 years ago).
-- Stas Malyshev smalyshev@gmail.com