php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #69028 Allow variable default function parameters
Submitted: 2015-02-11 11:21 UTC Modified: 2015-02-13 17:53 UTC
Votes:2
Avg. Score:4.5 ± 0.5
Reproduced:2 of 2 (100.0%)
Same Version:2 (100.0%)
Same OS:2 (100.0%)
From: martin at glooware dot com Assigned: ajf (profile)
Status: Duplicate Package: *General Issues
PHP Version: 5.6.0 OS: all
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: martin at glooware dot com
New email:
PHP Version: OS:

 

 [2015-02-11 11:21 UTC] 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.


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-02-11 18:26 UTC] ajf@php.net
-Status: Open +Status: Closed -PHP Version: Irrelevant +PHP Version: 5.6.0 -Assigned To: +Assigned To: ajf
 [2015-02-11 18:26 UTC] ajf@php.net
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 18:32 UTC] ajf@php.net
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-12 08:39 UTC] martin at glooware dot com
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...
 [2015-02-12 11:22 UTC] nikic@php.net
-Status: Closed +Status: Duplicate -Package: PHP Language Specification +Package: *General Issues
 [2015-02-12 11:22 UTC] nikic@php.net
Sorry for the grumpy response ;) As Andrea already mentioned herself, this isn't yet implemented in the way that was requested, so closing as duplicate of existing https://bugs.php.net/bug.php?id=60544 instead. In there is a link to an SO thread with some more thoughts on the topic, might be of interest.

I don't think the request to support any expression is unreasonable per se - this would only cause issues with Reflection, but doesn't have any semantic issues like allowing arbitrary expressions for property initializers etc.

There's a number of relatively common non-constant parameter defaults, e.g. I've often seen parameters declared as `Foo $param = null` with a `$param = $param ?: new DefaultFoo` line in the body (whether this is good code design is another question).
 [2015-02-12 11:25 UTC] ajf@php.net
Oh yeah, I apologise for being rather hostile before... It is a valid request, I was just surprised to see the first sample given we've supported it for a very, very long time.
 [2015-02-12 11:32 UTC] martin at martinambrus dot com
Thanks for the clarification and pointing me to a right direction. I too apologise for a little over-reacting with my last comment, I was only genuinely surprised when I saw the first reply.

Also, sorry about the constant thing, I've probably not tried that for 6 years. And now that the variable thing re-appeared in my mind, I guess I just wanted to prove a point with a wrong example.

Best of luck, guys :)
 [2015-02-13 17:53 UTC] pollita@php.net
Not for nothin', but HHVM supports any arbitrary expression in the default, even function calls.

function foo($bar, $baz = getBaz($bar)) { ... }
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Apr 20 09:01:28 2024 UTC