Re: [PHP-DEV] [Pre-RFC] Idea: named parameter lists
[email protected] (Henrik Skov)
| Newsgroups | php.internals |
|---|---|
| Message-ID | <[email protected]> |
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,
> ];
>
> function fakeSetCookie(
> string $name,
> string $value = "",
> int $expires_or_options = 0,
> string $path = "",
> string $domain = "",
> bool $secure = false,
> bool $httponly = false,
> string $samesite = "Strict",
> ) {
> var_dump([
> "name" => $name,
> "value" => $value,
> "expires_or_options" => $expires_or_options,
> "path" => $path,
> "domain" => $domain,
> "secure" => $secure,
> "httponly" => $httponly,
> "samesite" => $samesite,
> ]);
> }
>
> sleep(100);
>
var_dump(time()); // -> 1787047284
>
> fakeSetCookie("testName", "testValue", ...COOKIE_PARAMS); so
> expires_or_options become 1787050884
>
> But sadly, it does not work:
>
> Fatal error: Constant expression contains invalid operations in
> /tmp/x.php on line 2
>
> /Henrik
>
> On 18/08/2026 11.34, AllenJB wrote:
>> On 2026-08-18 10:12, Henrik Skov wrote:
>>>
>>> Hi list !
>>>
>>> Just got an idea:
>>>
>>> params COOKIE_PARAMS {
>>> time() + 3600, // Duration - NOTE: any expression is
>>> allowed here except pure variables
>>> '/', // Path
>>> '', // Can't remember what this is...
>>> (! IS_DEV), // secure — set true once served over
>>> HTTPS (production)
>>> TRUE, // httpOnly
>>> 'Lax', // Lax|Strict
>>> }
>>>
>>> Instead of this: (I am using Swoole)
>>>
>>> $response->cookie(
>>> self::COOKIE_NAME,
>>> $token,
>>> time() + 3600,
>>> '/',
>>> '',
>>> (! IS_DEV), // secure — set true once served over HTTPS
>>> TRUE, // httpOnly
>>> 'Lax',
>>> );
>>>
>>> we could do:
>>>
>>> $response->cookie(
>>> self::COOKIE_NAME,
>>> $token,
>>> :::COOKIE_PARAMS
>>> );
>>>
>>> I know it is kind of similar to the spread operator but not quite.
>>>
>>> What do you guys think ?Email:
>>>
>>
>> PHP already has named parameters[0], and this can be combined with
>> array unpacking[1] to produce basically the same as you're requesting
>> here: https://3v4l.org/kD27a#v
>>
>> (Note: I've purposely moved the samesite parameter to prove the named
>> array keys are mapping to the named function parameters, and not just
>> working because they happen to be in order)
>>
>> [0]
>> https://www.php.net/manual/en/functions.arguments.php#functions.named-arguments
>> [1]
>> https://www.php.net/manual/en/language.types.array.php#language.types.array.unpacking
>>
> --
>
>
>
> Med venlig hilsen
>
> Henrik Skov
> /HSK Consulting/
> Blegdamsvej 128B, 4
> DK-2100 Copenhagen O
> Tel.: +45 27 62 83 01
> Email: [email protected]
--
Med venlig hilsen
Henrik Skov
/HSK Consulting/
Blegdamsvej 128B, 4
DK-2100 Copenhagen O
Tel.: +45 27 62 83 01
Email: [email protected]