Re: [Pre-RFC] Idea: named parameter lists
| From: | AllenJB | Date: | Tue, 18 Aug 2026 10:52:45 +0000 |
| Subject: | Re: [Pre-RFC] Idea: named parameter lists | ||
| References: | 1 2 3 4 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-132251@lists.php.net to get a copy of this message | ||
On 2026-08-18 11:02, Henrik Skov wrote:
Hi again ! The important part is that any instantiations and function calls are evaluated at call-time and not compile time ! So time() would return the timestamp at time of call and not when compiler first encounters the COOKIE_PARAMS const array. The new params keyword would make this clear to the compiler /Henrik On 18/08/2026 11.43, Henrik Skov wrote:Ah, I think I understand now. What you're proposing is a structure where you can define a value as an expression, and that expression is only evaluated when the structure is used (rather than when it's defined). So, even in a long-lived application, the expires_or_options value would always be evaluated asThanks for the feedback. Problem with your re-written example is that it works with/depends on global variables... I was the code below actually worked: <?php const COOKIE_PARAMS = ["samesite" => "Lax", "expires_or_options" => time() + 3600, // Let's say time is 1787047184 "path" => "/", "domain" => "", "secure" => false, "httponly" => true,];sleep(100); var_dump(time()); // -> 1787047284 fakeSetCookie("testName", "testValue", ...COOKIE_PARAMS); so expires_or_options become 1787050884[...]
time() + 3600
If this were to be implemented, I think it would be better to look at values, rather than defining an entire structure. I can see it being used for more than parameters.
Something like an IIFE that's invoked when used rather than when defined.
As a side-note, one way to achieve this in current PHP would be to use a callable:
$params = function() {
return [
"samesite" => "Lax",
"expires_or_options" => time() + 3600,
"path" => "/",
"domain" => "",
"secure" => false,
"httponly" => true,
];
};
fakeSetCookie("testName", "testValue", ...$params());
sleep(2);
fakeSetCookie("testName", "testValue", ...$params());
(3v4l refused to save this, probably because it takes too long)
This can also be written using arrow functions:
$params = fn() => [
"samesite" => "Lax",
"expires_or_options" => time() + 3600,
"path" => "/",
"domain" => "",
"secure" => false,
"httponly" => true,
];