Req #69028 [Csd]: Allow variable default function parameters

[email protected] ("martin at glooware dot com")
Newsgroups php.standards
Message-ID <[email protected]>
Edit report at https://bugs.php.net/bug.php?id=69028&edit=1

 ID:                 69028
 User updated by:    martin at glooware dot com
 Reported by:        martin at glooware dot com
 Summary:            Allow variable default function parameters
 Status:             Closed
 Type:               Feature/Change Request
 Package:            PHP Language Specification
 Operating System:   all
 PHP Version:        5.6.0
 Assigned To:        ajf
 Block user comment: N
 Private report:     N

 New Comment:

Cool, my intention was definitely not to waste your time. I'm just not on 5.6 yet and I still don't see how the variables part is irrelevant, as it's not implemented and still stands as a feature request.

So now I wasted my time to request something that you just close the request for with an irritated comment. Thought a little bit more of you guys, but looks like it's just like with PHP - when you don't like something, you just throw an exception and send us to hell. Nice...


Previous Comments:
------------------------------------------------------------------------
[2015-02-11 18:32:40] [email protected]

For a more detailed answer:

>Currently it's not possible to use any constants, variables or anonymous functions in a function parameter as default value. This is for example not possible:

>function saveUser($name, $surname, $company = CONSTANT_COMPANY_VALUE) {}

Did you actually test that code sample and check the manual to make sure your assertion was correct‽

You have been able to use constants as default values since PHP 5.3, and that came out 6 years ago. That function you provided? It already works.

Plus, since PHP 5.6, constant expressions are supported:

    function foobar($x = "hello, " . [1, 2, 3][SOME_CONST] / 3 ** 7);

It's true that you can't use variables, but that can be easily done in the body of the function.

------------------------------------------------------------------------
[2015-02-11 18:26:37] [email protected]

Thank you for your bug report. This issue has already been fixed
in the latest released version of PHP, which you can download at 
http://www.php.net/downloads.php

Please do not waste our time by asking for features which have already been implemented.

http://wiki.php.net/rfc/const_scalar_exprs

------------------------------------------------------------------------
[2015-02-11 11:21:36] martin at glooware dot com

Description:
------------
Currently it's not possible to use any constants, variables or anonymous functions in a function parameter as default value. This is for example not possible:

function saveUser($name, $surname, $company = CONSTANT_COMPANY_VALUE) {}

... or even this ...

function calculateGravitationalPull($rotation, $axis, $speed, $grav_const = (3.234 / $rotation * $speed)) {}

I've come across various situations, like the example ones above, where I'd love to be able to inject a configuration value or an anonymous function as a function parameter. 

Coding this all inside the function is of course also possible. However, this would make it much easier to create some specific functions for which we'd be able to say how the function behaves straight from its own function and parameters documentation.



------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=69028&edit=1
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.