Re: [PHP-DEV] [Pre-RFC] Idea: named parameter lists
Kamil Tekiela <[email protected]>
| Newsgroups | gmane.comp.php.devel |
|---|---|
| Message-ID | <CAGBsUrfgzWA8ufeOxSpU-TZCGLe9nqmaMrNX9n1YXsbJQNVGMg@mail.gmail.com> |
On Tue, 18 Aug 2026 at 12:35, Henrik Skov <[email protected]> wrote: > > On 18/08/2026 12.52, AllenJB wrote: > > 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: > > > Thanks 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 > > 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 as `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: > > ```php > $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: > > ```php > $params = fn() => [ > "samesite" => "Lax", > "expires_or_options" => time() + 3600, > "path" => "/", > "domain" => "", > "secure" => false, > "httponly" => true, > ]; > ``` > > -- > > Yes, but I think one should consider params COOKIE_PARAMS {...} more of a type ! Then why not create a type for it https://3v4l.org/lNjSX#vgit.master