Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:125379 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 786D41A00BD for ; Sun, 1 Sep 2024 21:48:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1725227403; bh=+T3C9dQPA93fuX6MhfYEz6tkXeuRCap0uz7ewYrKhVQ=; h=From:Date:Subject:To:From; b=cGZ3+1CgiVrreRTm5yC7izZ8KNvC/3rL1fr/2T2axNKVgWL9XvGTUkCxkm2tfP5jG 4Mx3FJSdu8Ef9RtMc7C2hPpM8RfMiVcdLtfR3XBFpHk2+yzygEtfPLpH39gnvEp1Ci 5BkN7AXkzQ+CJH97NR3MzX7dB1dE+FxaPd6mEuDGTpMA4KmFiJfdOPFBO+j7AY/l16 TnPbyiTx+KyBECVclbniOFjv2YSZeiQvkgcSdq33FR3UyP+aoj9x0ZGOW+8/9DIHO7 FfxZ9vJXd+IbhruQU0H9g/vks+ygLru8/oHNOjdhaou9dl+HOVEKS6+JhLh1bLX7UC twP32O2caT3Kw== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id C3F4518007F for ; Sun, 1 Sep 2024 21:50:02 +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.6 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS,FREEMAIL_FROM, HTML_MESSAGE,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL, SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from mail-ot1-f52.google.com (mail-ot1-f52.google.com [209.85.210.52]) (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 ; Sun, 1 Sep 2024 21:50:02 +0000 (UTC) Received: by mail-ot1-f52.google.com with SMTP id 46e09a7af769-70f6e65d605so1198055a34.3 for ; Sun, 01 Sep 2024 14:48:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1725227285; x=1725832085; darn=lists.php.net; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=TuFUTZexcqIUY2YqWBUaEGt0qGNploVoOEHe+iEZZBQ=; b=B6yIVRj1tf8fuHylODjQJEZrc6wHXw8Rw5flF4cfqOqOw59Fdem/dI3/NV6N/DPM48 vVPiJgA6gp4kEenJTca3Fwxoqrjx7I/XjzHKlZdV51974yuB3M8kLKx9HtPYUaFB/NUB XyidHuzMDdB7svWgx/iwQCh+rldQ2tCjYHuuDtTCwpvYEIAG5MML9DzWjFkGSY0pnXO3 JNgomiX+qGyykUwpFHaj6o+XAKZwsZwFYeyDtR0+12wvT+oFQ7ve1mglpvzS7LMhY6S0 DkAkG/p98OXXnAw02cSeiiHnp7GAQr3ISCTkpXbuZphN/9kF/FggLaALXAGyJ/s/LN42 7jnw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725227285; x=1725832085; h=to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=TuFUTZexcqIUY2YqWBUaEGt0qGNploVoOEHe+iEZZBQ=; b=dBZeJf6xBQGTHe2Rs2ovkRuJhPhcBbFJMar5TxMpf73gyJPuyII56W7tUy4/RUoKgn CkM2cxjhO+KNDlI8PRYHjiQSxllhU/jN/aH/7F2m/SL9qh7qcYwZMb57YYBw1+fK7cqk HP/uD7fS6ejnILA43befvnxOE9CUxr6NcVHOzfqtYxXqmyo+GWso9QbxxcFt7aJ1s+9w hoMhV7ux7liMvCk/tBD1td9+nZevEOzhk6UOlY0fqiDEYU/p1zGvn780axA2I07D/ut7 psum/ZzhUwkQNU1LBY1L0dt1soTRGtGqTrvgbnCtFyL0oEzvTkVmFK+6vjfhpULH/dgG C7Dw== X-Gm-Message-State: AOJu0YyXIovRLdVnUCnhybNHP5LjAKyMh8SSp1HZyJ79ACMt/8lrUyRx 2vSVbxZRcNa6sFB/v/AJN8I1qrPlV9XQwdecpThNUcwfmdDNar7bZUkN+IeeLg1K8cazRPgkOQt PnEQNRRbj2BhOzLLSKuP4sTI/q5q4gtD7 X-Google-Smtp-Source: AGHT+IFCoi64ETu9oB+Owb+nYY7lVNWQcZzscTdBQ1mk3rkXOiIPXg6LULD7wMNnHh4lsi/0jWHlVr74pXMV1/AhlNI= X-Received: by 2002:a05:6830:2a07:b0:70f:3700:8286 with SMTP id 46e09a7af769-70f5c466fccmr14922815a34.24.1725227284993; Sun, 01 Sep 2024 14:48:04 -0700 (PDT) Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net x-ms-reactions: disallow MIME-Version: 1.0 Date: Sun, 1 Sep 2024 15:47:53 -0600 Message-ID: Subject: [PHP-DEV] Pre-RFC Discussion: Support for String Literals as Object Properties and Named Parameters in PHP To: internals@lists.php.net Content-Type: multipart/alternative; boundary="0000000000007a9d33062115c7c2" From: hamiegold@gmail.com (Hammed Ajao) --0000000000007a9d33062115c7c2 Content-Type: text/plain; charset="UTF-8" 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 when working with string literals. I'm looking forward to hearing your thoughts and feedback on this proposal. Introduction I'm suggesting two enhancements to PHP that I think could make our lives as developers a bit easier: Support for String Literals as Object Properties Support 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 non-standard characters 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 = "application/json"; } $obj = new Foo(); $obj->contentType = "text/html"; ``` But when we're dealing with data that includes non-standard characters or strings (think HTTP headers), we often end up using associative arrays: ```php $headers = [ "Content-Type" => "application/json", "X-Custom-Header" => "value" ]; ``` I think we can all agree that this reliance on arrays can make our code 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 { public function __construct( public string "Content-Type" = "application/json", public string "Cache-Control" = "no-cache, no-store, must-revalidate", public string "Pragma" = "no-cache", public string "Expires" = "0", public string "X-Frame-Options" = "SAMEORIGIN", public string "X-XSS-Protection" = "1; mode=block", public string "X-Content-Type-Options" = "nosniff", public string "Referrer-Policy" = "strict-origin-when-cross-origin", public string "Access-Control-Allow-Origin" = "*", public string "X-Custom-Header" = "value", ) {} public static function create(string ...$headers): self { return new self(...$headers); // Throws an error if an unknown named parameter is passed } public function dispatch(): void { foreach ((array) $this as $name => $value) { header("$name: $value"); } } } $headers = new MyHeaders("Content-Type": "application/json", "X-Custom-Header": "value"); // or $headers = MyHeaders::create("Content-Type": "text/html; charset=utf-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 representation. Benefits Greater Flexibility: We could create more natural and direct representations of data within objects. Enhanced Consistency: This aligns with the proposed support for string literals as named parameters, creating a more uniform language experience. Simplification: It could reduce our need for associative arrays, which can be more error-prone and less intuitive. 2. String Literals as Named Parameters in Function Calls If we're going to use string literals as object properties, it makes sense to also use them as named parameters, especially in constructors with 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 => $value) header("$key: $value"); } ``` To use string literals with special characters, we have to use associative arrays: ```php myHeaders(...["Content-Type" => "application/json"]); ``` 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 look something like this: ```php foo("Content-Type": "application/json"); ``` I think this syntax could offer several advantages: Improved Readability: Our code could become clearer and more aligned with natural data representation. Automatic Parameter Mapping: We could map string literals to corresponding parameters without requiring manual intervention. Simplified Constructor Usage: This could be especially beneficial for constructors where we need to pass complex data structures directly. Implementation Considerations Of course, implementing these changes would require some work: The PHP engine would need to accommodate string literals as valid property names and named parameters, both at runtime and in class/function definitions. We'd need to carefully manage compatibility with existing code to ensure 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 constructors with promoted properties. 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 --0000000000007a9d33062115c7c2 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Dear PHP internals community,
I hope this email finds y= ou all well. I'd like to propose an idea that I believe could enhance P= HP's flexibility and consistency, especially when working with string l= iterals. I'm looking forward to hearing your thoughts and feedback on t= his proposal.
Introduction
I'm suggesting two enhancements to PHP= that I think could make our lives as developers a bit easier:

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

The main goal here is to reduc= e our reliance on arrays and provide more intuitive ways to define and acce= ss data, particularly in scenarios like working with HTTP headers where we = often deal with non-standard characters and strings.
1. String Literals = as Object Properties
Current Situation
As we all know, we typically d= efine and access object properties using standard identifiers:
```phpclass Foo {
=C2=A0 =C2=A0 public string $contentType =3D "applicat= ion/json";
}

$obj =3D new Foo();
$obj->contentType =3D= "text/html";
```

But when we're dealing with data = that includes non-standard characters or strings (think HTTP headers), we o= ften end up using associative arrays:
```php
$headers =3D [
=C2=A0= =C2=A0 "Content-Type" =3D> "application/json",
= =C2=A0 =C2=A0 "X-Custom-Header" =3D> "value"
];```

I think we can all agree that this reliance on arrays can make= our code less intuitive, especially when we're managing complex data s= tructures.
Proposed Enhancement
What if we could use string literals = as object property names? Here's what I'm thinking:
```php
cl= ass MyHeaders {

=C2=A0 =C2=A0 public function __construct(
=C2=A0= =C2=A0 =C2=A0 =C2=A0 public string "Content-Type" =3D "appl= ication/json",
=C2=A0 =C2=A0 =C2=A0 =C2=A0 public string "Cach= e-Control" =3D "no-cache, no-store, must-revalidate",
=C2= =A0 =C2=A0 =C2=A0 =C2=A0 public string "Pragma" =3D "no-cach= e",
=C2=A0 =C2=A0 =C2=A0 =C2=A0 public string "Expires" = =3D "0",
=C2=A0 =C2=A0 =C2=A0 =C2=A0 public string "X-Fra= me-Options" =3D "SAMEORIGIN",
=C2=A0 =C2=A0 =C2=A0 =C2=A0= public string "X-XSS-Protection" =3D "1; mode=3Dblock"= ,
=C2=A0 =C2=A0 =C2=A0 =C2=A0 public string "X-Content-Type-Options= " =3D "nosniff",
=C2=A0 =C2=A0 =C2=A0 =C2=A0 public strin= g "Referrer-Policy" =3D "strict-origin-when-cross-origin&quo= t;,
=C2=A0 =C2=A0 =C2=A0 =C2=A0 public string "Access-Control-Allow= -Origin" =3D "*",
=C2=A0 =C2=A0 =C2=A0 =C2=A0 public stri= ng "X-Custom-Header" =3D "value",
=C2=A0 =C2=A0 ) {}=

=C2=A0 =C2=A0 public static function create(string ...$headers): se= lf {
=C2=A0 =C2=A0 =C2=A0 =C2=A0 return new self(...$headers); // Throws= an error if an unknown named parameter is passed
=C2=A0 =C2=A0 }
=C2=A0 =C2=A0 public function dispatch(): void {
=C2=A0 =C2=A0 =C2=A0 = =C2=A0 foreach ((array) $this as $name =3D> $value) {
=C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 header("$name: $value");
=C2=A0 = =C2=A0 =C2=A0 =C2=A0 }
=C2=A0 =C2=A0 }
}

$headers =3D new MyHe= aders("Content-Type": "application/json", "X-Custo= m-Header": "value");
// or
$headers =3D MyHeaders::cre= ate("Content-Type": "text/html; charset=3Dutf-8", "= ;X-Custom-Header": "value");
$headers->dispatch();
= ```
This would allow us to include characters in property names that are= n't typically allowed in PHP identifiers, like hyphens or spaces. I thi= nk this could make our code more readable and aligned with natural data rep= resentation.
Benefits

Greater Flexibility: We could create more n= atural and direct representations of data within objects.
Enhanced Consi= stency: This aligns with the proposed support for string literals as named = parameters, creating a more uniform language experience.
Simplification:= It could reduce our need for associative arrays, which can be more error-p= rone and less intuitive.

2. String Literals as Named Parameters in F= unction Calls
If we're going to use string literals as object proper= ties, it makes sense to also use them as named parameters, especially in co= nstructors with promoted properties. And why stop at constructors? This lea= ds to the second part of my proposal.
Current Situation
We can use na= med parameters in function calls, but only with standard identifiers:
``= `php
function myHeaders(...$args) {
=C2=A0 =C2=A0 foreach ($args as $= key =3D> $value) header("$key: $value");
}
```

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

This can be a bit cumbersome and less = readable, especially for complex data structures.
Proposed EnhancementWhat if we could use string literals as named parameters? It might look s= omething like this:
```php
foo("Content-Type": "applic= ation/json");
```

I think this syntax could offer several ad= vantages:

Improved Readability: Our code could become clearer and mo= re aligned with natural data representation.
Automatic Parameter Mapping= : We could map string literals to corresponding parameters without requirin= g manual intervention.
Simplified Constructor Usage: This could be espec= ially beneficial for constructors where we need to pass complex data struct= ures directly.

Implementation Considerations
Of course, implement= ing these changes would require some work:

The PHP engine would need= to accommodate string literals as valid property names and named parameter= s, both at runtime and in class/function definitions.
We'd need to c= arefully manage compatibility with existing code to ensure traditional prop= erty access remains unaffected.
We'd need to decide whether to allow= string literals as parameters in function/method declarations or only in f= unction calls (to be retrieved by func_get_args() or variadic functions), w= ith exceptions for constructors with promoted properties.

I'm re= ally 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 t= hink this could impact your day-to-day coding?
Looking forward to a grea= t discussion!
Best regards,
Hammed
--0000000000007a9d33062115c7c2--