Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:125394 X-Original-To: internals@lists.php.net Delivered-To: internals@lists.php.net Received: from php-smtp4.php.net (php-smtp4.php.net [45.112.84.5]) by qa.php.net (Postfix) with ESMTPS id 6C96C1A00BD for ; Mon, 2 Sep 2024 19:49:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1725306702; bh=+X4VlYFajYSEzt+UL2GA27S04iFrc5N3EptVI3J1L+w=; h=Date:From:To:In-Reply-To:References:Subject:From; b=Ne91YBp94fsotnKO8r/6eSc2ftj4RkWIluJgkimSDfbZ51o94/UHEo+pBav6EP2px CgGWw50lNtX9s49D7C2dznJXRbVZUYiV8SQFrdXfwCeMWoMB5VFVy+d40+nws0UkuG Qg21PJKanqp13fJNuxZvjFd4N5S5Kt0/i1QP/bMczxsLOA9IhvKFIgPvQ1Chr81xJ+ pYKvXIwqgIU4vgY39ZtU/TYGQb1KRmln7uLyESdRgHHG8vXd4Zn8arliYqe9FeHgGm V3LT/JPuCQX98LpnIlcyLXb5yYcA+Z7GMuAzr8qyLP2OzpmDpmD7ZRurMuiWK3udCR mQwligv4Hxfdg== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 72EDB180068 for ; Mon, 2 Sep 2024 19:51:41 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.0 (2022-12-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-0.1 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_MISSING,HTML_MESSAGE, RCVD_IN_DNSWL_LOW,RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,SPF_HELO_PASS, SPF_PASS autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from fhigh6-smtp.messagingengine.com (fhigh6-smtp.messagingengine.com [103.168.172.157]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Mon, 2 Sep 2024 19:51:40 +0000 (UTC) Received: from phl-compute-03.internal (phl-compute-03.nyi.internal [10.202.2.43]) by mailfhigh.nyi.internal (Postfix) with ESMTP id 7880511401A4 for ; Mon, 2 Sep 2024 15:49:43 -0400 (EDT) Received: from phl-imap-09 ([10.202.2.99]) by phl-compute-03.internal (MEProxy); Mon, 02 Sep 2024 15:49:43 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bottled.codes; h=cc:content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm2; t=1725306583; x=1725392983; bh=LRd63tIjco uhkhrYAzH9KQ5nUghsDmS/Rxtil6nBGGU=; b=Ym3+prAYvAgI0QKBFF5yv0OV9B Yc/4US06sOQdg7/xzatpVZLP298pqrS68LV62DmIAGiYilLe/RRA4izhQ77k247z 6FFOggtmqRwu49nex2amkuGeC7/atM3aE9EUh5Ncsul5OG7wKd6gOYmxWC05Q78S Bqe8hEVhe2r+k8eAh6RnyKQUnMriM0aF9A/aZF+AIF3DxKhdfoU9tJx1D8ZqOvtG EEU/rF7YKMwJ3EKftOhUBUBiJbX5FHHS7JtcT0ZXYDwvxzJxxTRRkx7CPDSZ2l+S AlRULl2hSFzl8cEtRu+/9G3dQHAsgj6UhzHuxB5LsoD3GLrAlCLHI+0ju5sA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm1; t=1725306583; x=1725392983; bh=LRd63tIjcouhkhrYAzH9KQ5nUghs DmS/Rxtil6nBGGU=; b=eRFxwaWL0Qiml24PTo22n30mO4iH+m8UavkXMOaho7tL ff4QFQQxbcixIK1s8zPdgLSoVWHIzYnjZ+qV9wtu1Etrxstn76Xy+t3lrBynM2bO 8NkGLFQCF9EbHayt5PXNaiZxqB3K1x3hfpajg9lACV5kuK/xykdIm/wFWgE53/BM R0JifzEAdjK/lBlYLu5Pwu5vaVcdwJbfGjt6EtV4j8cPWiwzAv5kBJzIn+jlMGJo 7pL3RpLB4dkxPH/IH+vZ/2IV6UaeAXuYKu/3l4OgSy1j0IhTgkBMVDFUbSVDQ6Go izBdmqICcnZqTXpvyg0ceTXegJX8bDAkghs111+WnA== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrudehfedgudegudcutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpggftfghnshhusghstghrihgsvgdp uffrtefokffrpgfnqfghnecuuegrihhlohhuthemuceftddtnecuogfuuhhsphgvtghtff homhgrihhnucdlgeelmdenucfjughrpefoggffhffvkfgjfhfutgesrgdtreerredtjeen ucfhrhhomhepfdftohgsucfnrghnuggvrhhsfdcuoehrohgssegsohhtthhlvggurdgtoh guvghsqeenucggtffrrghtthgvrhhnpedtiedtvddvvefhudffhfegleffteegffevkeeh keefleeuuddtieevkedvteejvdenucffohhmrghinhepfehvgehlrdhorhhgnecuvehluh hsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomheprhhosgessghothht lhgvugdrtghouggvshdpnhgspghrtghpthhtohepuddpmhhouggvpehsmhhtphhouhhtpd hrtghpthhtohepihhnthgvrhhnrghlsheslhhishhtshdrphhhphdrnhgvth X-ME-Proxy: Feedback-ID: ifab94697:Fastmail Received: by mailuser.nyi.internal (Postfix, from userid 501) id 29416780067; Mon, 2 Sep 2024 15:49:43 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net x-ms-reactions: disallow MIME-Version: 1.0 Date: Mon, 02 Sep 2024 21:49:22 +0200 To: internals@lists.php.net Message-ID: <0418049b-7981-40a0-a0f9-2430eceb7d12@app.fastmail.com> In-Reply-To: References: Subject: Re: [PHP-DEV] Pre-RFC Discussion: Support for String Literals as Object Properties and Named Parameters in PHP Content-Type: multipart/alternative; boundary=ddba43b12dc441fe91ef6e8dc737f87a From: rob@bottled.codes ("Rob Landers") --ddba43b12dc441fe91ef6e8dc737f87a Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable On Sun, Sep 1, 2024, at 23:47, Hammed Ajao wrote: > Dear PHP internals community, > I hope this email finds you all well. I'd like to propose an idea that= I believe could enhance PHP's flexibility and consistency, especially w= hen working with string literals. I'm looking forward to hearing your th= oughts and feedback on this proposal. > Introduction > I'm suggesting two enhancements to PHP that I think could make our liv= es as developers a bit easier: >=20 > Support for String Literals as Object Properties > Support for String Literals as Named Parameters in Function Calls >=20 > The main goal here is to reduce our reliance on arrays and provide mor= e intuitive ways to define and access data, particularly in scenarios li= ke working with HTTP headers where we often deal with non-standard chara= cters and strings. > 1. String Literals as Object Properties > Current Situation > As we all know, we typically define and access object properties using= standard identifiers: > ```php > class Foo { > public string $contentType =3D "application/json"; > } >=20 > $obj =3D new Foo(); > $obj->contentType =3D "text/html"; > ``` >=20 > But when we're dealing with data that includes non-standard characters= or strings (think HTTP headers), we often end up using associative arra= ys: > ```php > $headers =3D [ > "Content-Type" =3D> "application/json", > "X-Custom-Header" =3D> "value" > ]; > ``` >=20 > I think we can all agree that this reliance on arrays can make our cod= e less intuitive, especially when we're managing complex data structures. > Proposed Enhancement > What if we could use string literals as object property names? Here's = what I'm thinking: > ```php > class MyHeaders { >=20 > public function __construct( > public string "Content-Type" =3D "application/json", > public string "Cache-Control" =3D "no-cache, no-store, must-re= validate", > public string "Pragma" =3D "no-cache", > public string "Expires" =3D "0", > public string "X-Frame-Options" =3D "SAMEORIGIN", > public string "X-XSS-Protection" =3D "1; mode=3Dblock", > public string "X-Content-Type-Options" =3D "nosniff", > public string "Referrer-Policy" =3D "strict-origin-when-cross-= origin", > public string "Access-Control-Allow-Origin" =3D "*", > public string "X-Custom-Header" =3D "value", > ) {} >=20 > public static function create(string ...$headers): self { > return new self(...$headers); // Throws an error if an unknown= named parameter is passed > } >=20 > public function dispatch(): void { > foreach ((array) $this as $name =3D> $value) { > header("$name: $value"); > } > } > } >=20 > $headers =3D new MyHeaders("Content-Type": "application/json", "X-Cust= om-Header": "value"); > // or > $headers =3D MyHeaders::create("Content-Type": "text/html; charset=3Du= tf-8", "X-Custom-Header": "value"); > $headers->dispatch(); > ``` > This would allow us to include characters in property names that aren'= t typically allowed in PHP identifiers, like hyphens or spaces. I think = this could make our code more readable and aligned with natural data rep= resentation. > Benefits >=20 > Greater Flexibility: We could create more natural and direct represent= ations of data within objects. > Enhanced Consistency: This aligns with the proposed support for string= literals as named parameters, creating a more uniform language experien= ce. > Simplification: It could reduce our need for associative arrays, which= can be more error-prone and less intuitive. >=20 > 2. String Literals as Named Parameters in Function Calls > If we're going to use string literals as object properties, it makes s= ense to also use them as named parameters, especially in constructors wi= th promoted properties. And why stop at constructors? This leads to the = second part of my proposal. > Current Situation > We can use named parameters in function calls, but only with standard = identifiers: > ```php > function myHeaders(...$args) { > foreach ($args as $key =3D> $value) header("$key: $value"); > } > ``` >=20 > To use string literals with special characters, we have to use associa= tive arrays: > ```php > myHeaders(...["Content-Type" =3D> "application/json"]); > ``` >=20 > This can be a bit cumbersome and less readable, especially for complex= data structures. > Proposed Enhancement > What if we could use string literals as named parameters? It might loo= k something like this: > ```php > foo("Content-Type": "application/json"); > ``` >=20 > I think this syntax could offer several advantages: >=20 > Improved Readability: Our code could become clearer and more aligned w= ith natural data representation. > Automatic Parameter Mapping: We could map string literals to correspon= ding parameters without requiring manual intervention. > Simplified Constructor Usage: This could be especially beneficial for = constructors where we need to pass complex data structures directly. >=20 > Implementation Considerations > Of course, implementing these changes would require some work: >=20 > The PHP engine would need to accommodate string literals as valid prop= erty names and named parameters, both at runtime and in class/function d= efinitions. > We'd need to carefully manage compatibility with existing code to ensu= re traditional property access remains unaffected. > We'd need to decide whether to allow string literals as parameters in = function/method declarations or only in function calls (to be retrieved = by func_get_args() or variadic functions), with exceptions for construct= ors with promoted properties. >=20 > I'm really interested to hear what you all think about this proposal. = Do you see potential benefits or challenges that I might have overlooked= ? How do you think this could impact your day-to-day coding? > Looking forward to a great discussion! > Best regards, > Hammed Hey Hammed, This gets into the parser and its definition of "LABEL" (below PHP's syn= tax): LABEL [a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]* This means the first letter must be alpha-numeric ascii, or higher. And = you can actually just use the latter part and whatever encoding your fil= e is (usually utf8): For example, this is valid PHP: class HTML { public string $Content=E2=80=93Type; public string $=E1=8B=90xl; public string $=F0=9D=9F=B70sm; } https://3v4l.org/KgJKm You can also use emojis or look-alikes as much as you want. It's not a d= irect answer to what you are seeking, but it gets pretty close. =E2=80=94 Rob --ddba43b12dc441fe91ef6e8dc737f87a Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: quoted-printable
On Sun, Sep 1, = 2024, at 23:47, Hammed Ajao wrote:
Dear PHP internals community,
I hope this email finds you all well. I'd like to propose a= n idea that I believe could enhance PHP's flexibility and consistency, e= specially when working with string literals. I'm looking forward to hear= ing your thoughts and feedback on this proposal.
Introduct= ion
I'm suggesting two enhancements to PHP that I think co= uld make our lives as developers a bit easier:

<= div>Support for String Literals as Object Properties
Suppo= rt for String Literals as Named Parameters in Function Calls

The main goal here is to reduce our reliance on arrays= and provide more intuitive ways to define and access data, particularly= in scenarios like working with HTTP headers where we often deal with no= n-standard characters and strings.
1. String Literals as O= bject Properties
Current Situation
As we all= know, we typically define and access object properties using standard i= dentifiers:
```php
class Foo {
    public string $contentType =3D "application/json";
}

$obj =3D new Foo();
$obj->contentType =3D "text/html";
```
<= br>
But when we're dealing with data that includes non-standar= d characters or strings (think HTTP headers), we often end up using asso= ciative arrays:
```php
$headers =3D [
    "Content-Type" =3D> "application/json",
    "X-Custom-Header" =3D> "value"
];<= br>
```

I think we can all agree = that this reliance on arrays can make our code less intuitive, especiall= y when we're managing complex data structures.
Proposed En= hancement
What if we could use string literals as object p= roperty names? Here's what I'm thinking:
```php
<= div>class MyHeaders {

    public = function __construct(
        public s= tring "Content-Type" =3D "application/json",
   =     public string "Cache-Control" =3D "no-cache, no-store, mu= st-revalidate",
        public string = "Pragma" =3D "no-cache",
        publi= c string "Expires" =3D "0",
        pu= blic string "X-Frame-Options" =3D "SAMEORIGIN",
  &nb= sp;     public string "X-XSS-Protection" =3D "1; mode=3Dblock"= ,
        public string "X-Content-Typ= e-Options" =3D "nosniff",
        publ= ic string "Referrer-Policy" =3D "strict-origin-when-cross-origin",
        public string "Access-Control-Allow= -Origin" =3D "*",
        public strin= g "X-Custom-Header" =3D "value",
    ) {}

    public static function create(strin= g ...$headers): self {
        return = new self(...$headers); // Throws an error if an unknown named parameter = is passed
    }

&nb= sp;   public function dispatch(): void {
   = ;     foreach ((array) $this as $name =3D> $value) {
            header("$name: $value"= );
        }
  &nbs= p; }
}

$headers =3D new MyHea= ders("Content-Type": "application/json", "X-Custom-Header": "value");
// or
$headers =3D MyHeaders::create("Content-= Type": "text/html; charset=3Dutf-8", "X-Custom-Header": "value");
$headers->dispatch();
```
This wo= uld allow us to include characters in property names that aren't typical= ly allowed in PHP identifiers, like hyphens or spaces. I think this coul= d make our code more readable and aligned with natural data representati= on.
Benefits

Greater Flexibil= ity: We could create more natural and direct representations of data wit= hin objects.
Enhanced Consistency: This aligns with the pr= oposed support for string literals as named parameters, creating a more = uniform language experience.
Simplification: It could redu= ce our need for associative arrays, which can be more error-prone and le= ss intuitive.

2. String Literals as Named P= arameters in Function Calls
If we're going to use string l= iterals as object properties, it makes sense to also use them as named p= arameters, especially in constructors with promoted properties. And why = stop at constructors? This leads to the second part of my proposal.
<= /div>
Current Situation
We can use named parameters in= function calls, but only with standard identifiers:
```ph= p
function myHeaders(...$args) {
  &nbs= p; foreach ($args as $key =3D> $value) header("$key: $value");
}
```

To use string l= iterals with special characters, we have to use associative arrays:
<= /div>
```php
myHeaders(...["Content-Type" =3D> "app= lication/json"]);
```

This ca= n be a bit cumbersome and less readable, especially for complex data str= uctures.
Proposed Enhancement
What if we cou= ld use string literals as named parameters? It might look something like= this:
```php
foo("Content-Type": "applicati= on/json");
```

I think this s= yntax could offer several advantages:

Impro= ved Readability: Our code could become clearer and more aligned with nat= ural data representation.
Automatic Parameter Mapping: We = could map string literals to corresponding parameters without requiring = manual intervention.
Simplified Constructor Usage: This co= uld be especially beneficial for constructors where we need to pass comp= lex data structures directly.

Implementatio= n Considerations
Of course, implementing these changes wou= ld require some work:

The PHP engine would = need to accommodate string literals as valid property names and named pa= rameters, both at runtime and in class/function definitions.
We'd need to carefully manage compatibility with existing code to ens= ure traditional property access remains unaffected.
We'd n= eed to decide whether to allow string literals as parameters in function= /method declarations or only in function calls (to be retrieved by func_= get_args() or variadic functions), with exceptions for constructors with= promoted properties.

I'm really interested= to hear what you all think about this proposal. Do you see potential be= nefits or challenges that I might have overlooked? How do you think this= could impact your day-to-day coding?
Looking forward to a= great discussion!
Best regards,
Hammed
<= /div>

Hey Hammed,
This gets into the parser and its definition of "LABEL" (be= low PHP's syntax):

LABEL [a-zA-Z_\x80-\xff]= [a-zA-Z0-9_\x80-\xff]*

This means the first= letter must be alpha-numeric ascii, or higher. And you can actually jus= t use the latter part and whatever encoding your file is (usually utf8):=

For example, this is valid PHP:
<= div>
class HTML {
    public = string $Content=E2=80=93Type;
    public st= ring $=E1=8B=90xl;
    public string $=F0=9D= =9F=B70sm;
}


You can also use emojis or look-alikes as much as you want. It's not = a direct answer to what you are seeking, but it gets pretty close.

=E2=80=94 Rob
--ddba43b12dc441fe91ef6e8dc737f87a--