Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:129205 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 lists.php.net (Postfix) with ESMTPS id EFC4D1A00BC for ; Wed, 12 Nov 2025 11:36:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1762947377; bh=ZHh41m4tIxLuLKPXhfldH1Yu4Ve2UWCyBhX3CFsRIyw=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=B6tX2ZR/GB3EqSRsN45z77PyPc1ANbEy65rxtnpf6xmOZdSzEomP+1hSL/f0bmDFH xg9tbQQtYfqeiiR1tewScLgUDBqg/RiZQQM7Pwej+iDQGRGI859jrma+OOgbGID1kN rXxR8BdDagaPky7HbaptnPpWGatbGVSQgrlIruY73zjM5uhzIWxMmBaqzNLVAbSBzb VYdhyRRPkWOKJUScDt/QlkexighgTOwl7pVXE9Kj9OjfiGqXWRgg1XGm03I0nKUDOm ACA673RcdK9Jntey5x1wwhUv9l7d3sEujKIDT0AwbLjCMYnmQjVWQ7ICd6sYh0bkCN PmD2j/mrEFuAw== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id BD645180041 for ; Wed, 12 Nov 2025 11:36:10 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-0.2 required=5.0 tests=BAYES_40,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.1 X-Spam-Virus: No X-Envelope-From: Received: from mail-ej1-f49.google.com (mail-ej1-f49.google.com [209.85.218.49]) (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 ; Wed, 12 Nov 2025 11:36:10 +0000 (UTC) Received: by mail-ej1-f49.google.com with SMTP id a640c23a62f3a-b472842981fso103707866b.1 for ; Wed, 12 Nov 2025 03:36:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1762947364; x=1763552164; darn=lists.php.net; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=0jXwTdA6ITh4VjYFVlTtxwkATOkm45FX63iTm1fXhN4=; b=CuffWhOJV4W/8B5gPNH/F+ZD5mvTUevQbm8bkW8k7QXvTB6zi7wq0xRQsWG/F9e9n1 YivO9JwoRO9Sz4iJRRNBoVLW5e9mWFVbpCkVfENNSU0FyCDAzExlnMmUOe6XTmdRURz1 t8B4E4WtbHa4m8ii7/BsKf8kM9KSQAleGyWg/LbpZlUnP4iRlFRcHLB0J2/JBwsi8KV4 KqrlMg3VLDZdkY81LOpSuWZA3T1kchmnk8RBS1sPnkaXNE0zURbO/uyDj0A1VJGEu/B0 LBpMaqSS/9X8jEoct41s51BD+mSJYQKsrZRLF2sRQ8VmBz454zzM0xAiYf1w5c3ICykc d4Ug== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1762947364; x=1763552164; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=0jXwTdA6ITh4VjYFVlTtxwkATOkm45FX63iTm1fXhN4=; b=vSnnoG/VvKxEGFK3eehWxO8ccPnDrKYDpX2OmwMaYIjEqHewbRnx63ByfOPKd/jVEc yWxrdvO8OpZoCSAqxwvIH2a8YlIAYRlK8fGU5cn2iKtnGJO5r7AwJccWSI976Rof5XsE lkHduei/+1sMQqtzCCpLAK0Htb8lWq9Px4OSuVvnDto849jLF8sQnDbKgLIvSyj7fr6x M+6zEQWZZz3tZDvRmWvxLM2t3JhxMbzWdvNgvyzE+HtA6WrCXmGWjYaCg4e0dTHM5YPf O56we6hUp+jHVtjmIEBUeL7JsJ4EnVROPAHNkKPcLj9v7jJ0rVao6SsX5bmf7A0AG+0t eNMg== X-Forwarded-Encrypted: i=1; AJvYcCXXv2b4gt+dupPEEZu3G1eeh2lHVJFriIn4fxyh9caywSIBQ2tRRFpctqE+xtZ5U2kz7F6vpUVWcik=@lists.php.net X-Gm-Message-State: AOJu0Yz0Z0eucVqAz1Jb8FoDbhDDOkJ66OCO0Uy/flO0PmydU6NW6Vtj fEbIpl+wrTpFRuVRnI7fkmmm9ghlZDBIogFu9UmxLo/qqTipjVISKjWs3MiyrqS4y8Eps5D+wNA k/ZeK8Kp3RWon33xvri1rZljcdaE0Z1o= X-Gm-Gg: ASbGncs+P5jqTg6I+4B32xL7CHNQorl/Qu1jqdvRe4h9N/AttsDPjLlOafBgWYKqoml s7vwy9heNVeMTsEIrIyQ297MuzLgYtnSFoyW3ZCvOoh93UEOU9I3ixPO+f5lu3KxHZ3bKQ76U/N kFFtyU79jgoIiwQ8C2fXgAwsnC3BkTrj6HkFva+HJVo4RTHt/pSzpg3MkK5PoByyXwKZ67QOxUy MZtZVCM/kppDxGOTm/U/fzbdv+PSq3P8w9zCwAEdzcEoCDKeH59lPvS/bjW X-Google-Smtp-Source: AGHT+IHsLrleeOgbopyoDEaYQbVMEup1SKj4lgpM3QPSunhOgV91vRl8dHGqGa2EyOTv4vqBKQ2S1iBroNceRL3zUxE= X-Received: by 2002:a17:906:7315:b0:b73:2b08:ac85 with SMTP id a640c23a62f3a-b733197edaamr232996566b.19.1762947364081; Wed, 12 Nov 2025 03:36:04 -0800 (PST) Precedence: list list-help: list-unsubscribe: list-post: List-Id: x-ms-reactions: disallow MIME-Version: 1.0 References: <34c08e21-8cd1-2e0e-1cca-148d4acc5432@php.net> In-Reply-To: <34c08e21-8cd1-2e0e-1cca-148d4acc5432@php.net> Date: Wed, 12 Nov 2025 14:35:52 +0300 X-Gm-Features: AWmQ_bmyUzUDX-nQz9JejUPBZNUq3FTt025DUgR19os0RpLtpCJ-VP-jPNmmlaA Message-ID: Subject: Re: [PHP-DEV] [RFC] [Discussion] Add values() Method to BackedEnum To: Derick Rethans Cc: Valentin Udaltsov , php internals Content-Type: multipart/alternative; boundary="00000000000064d3a80643642b5f" From: mikhail.d.savin@gmail.com (Mikhail Savin) --00000000000064d3a80643642b5f Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable =D0=B2=D1=82, 11 =D0=BD=D0=BE=D1=8F=D0=B1. 2025=E2=80=AF=D0=B3. =D0=B2 14:5= 0, Derick Rethans : > On Tue, 11 Nov 2025, Mikhail Savin wrote: > > > The RFC addresses this by adding "public static function values(): arra= y" > > to the BackedEnum interface itself. > > With that in place, an enum that defines an incompatible values() will > fail > > at compile time with a normal method-signature incompatibility, exactly > > like it would for from()/tryFrom(). > > > > Here=E2=80=99s a .phpt demonstrating the intended behavior: > > > > --TEST-- > > Backed enums: user-defined values() incompatible with interface signatu= re > > --FILE-- > > > > > enum E: string { > > case A =3D 'a'; > > > > // Intentional incompatibility: interface requires array return typ= e > > public static function values(): string { > > return 'values'; > > } > > } > > > > ?> > > --EXPECTF-- > > Fatal error: Declaration of E::values(): string must be compatible with > > BackedEnum::values(): array in %s on line %d > > Right now, enums can have a "values()" function that can return anything > I desire: an array of enum cases, or objects created from these, or > strings, or M_PI. > > If it has to be compatible with > > `BackedEnum::values(): array(int|string)` > > (which is what your contract demands, even though it can't express the > int|string part) > > Then that is a BC break again. > > cheers, > Derick Hi, guys, thanks for feedback Following the discussion with Alex and Derick, I've completed a comprehensive BC analysis. I need community input on a key decision: whether to include a return type in the interface declaration. ## The Two Options **Option A: WITH return type (current PR)** ```php interface BackedEnum { public static function values(): array; } ``` - BC Breaks: 71-600 implementations (1.0-8.8%) - Better type safety and IDE support - Consistent with cases(): array **Option B: WITHOUT return type (alternative)** ```php interface BackedEnum { public static function values(); } ``` - BC Breaks: 0 (0%) - All existing implementations compatible - Can add `: array` in PHP 9.0 ## BC Analysis Data Total enums with values(): 6,800 - Compatible (: array): 6,200 (91.2%) - Missing return type: 64 (0.9%) - Wrong return types: 7 (0.1%) - Unaccounted: ~529 (7.8%) All GitHub search links: https://github.com/php/php-src/pull/20398 ## Question for Community Which approach should we take for PHP 8.6? **Option A:** Accept 1-9% BC break for full type safety **Option B:** Zero BC breaks, add typing in PHP 9.0 I'm inclined toward Option B (zero breaks for 8.6), but want to hear community preference before changing the implementation. Thoughts? --- Additional context: - Implementation change is trivial (one line) - Native implementation returns array regardless of interface - Alex's array_column suggestion incorporated (+3.6k usages) - All data verifiable via GitHub searches in PR Best regards, Mikhail --00000000000064d3a80643642b5f Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable


=D0=B2=D1=82, 1= 1 =D0=BD=D0=BE=D1=8F=D0=B1. 2025=E2=80=AF=D0=B3. =D0=B2 14:50, Derick Retha= ns <derick@php.net>:
<= blockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;border-l= eft:1px solid rgb(204,204,204);padding-left:1ex">On Tue, 11 Nov 2025, Mikha= il Savin wrote:

> The RFC addresses this by adding "public static function values()= : array"
> to the BackedEnum interface itself.
> With that in place, an enum that defines an incompatible values() will= fail
> at compile time with a normal method-signature incompatibility, exactl= y
> like it would for from()/tryFrom().
>
> Here=E2=80=99s a .phpt demonstrating the intended behavior:
>
> --TEST--
> Backed enums: user-defined values() incompatible with interface signat= ure
> --FILE--
> <?php
>
> enum E: string {
>=C2=A0 =C2=A0 =C2=A0case A =3D 'a';
>
>=C2=A0 =C2=A0 =C2=A0// Intentional incompatibility: interface requires = array return type
>=C2=A0 =C2=A0 =C2=A0public static function values(): string {
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0return 'values';
>=C2=A0 =C2=A0 =C2=A0}
> }
>
> ?>
> --EXPECTF--
> Fatal error: Declaration of E::values(): string must be compatible wit= h
> BackedEnum::values(): array in %s on line %d

Right now, enums can have a "values()" function that can return a= nything
I desire: an array of enum cases, or objects created from these, or
strings, or M_PI.

If it has to be compatible with

=C2=A0 =C2=A0 =C2=A0 =C2=A0 `BackedEnum::values(): array(int|string)`

(which is what your contract demands, even though it can't express the =
int|string part)

Then that is a BC break again.

cheers,
Derick

Hi, guys, thanks for feedback

Foll= owing the discussion with Alex and Derick, I've completed a comprehensi= ve BC
analysis. I need community input on a key decision: whether to in= clude a return
type in the interface declaration.

## The Two Opt= ions

**Option A: WITH return type (current PR)**
```php
interf= ace BackedEnum {
=C2=A0 =C2=A0 public static function values(): array;}
```
- BC Breaks: 71-600 implementations (1.0-8.8%)
- Better ty= pe safety and IDE support
- Consistent with cases(): array

**Opti= on B: WITHOUT return type (alternative)**
```php
interface BackedEnum= {
=C2=A0 =C2=A0 public static function values();
}
```
- BC Br= eaks: 0 (0%)
- All existing implementations compatible
- Can add `: a= rray` in PHP 9.0

## BC Analysis Data

Total enums with values(= ): 6,800
- Compatible (: array): 6,200 (91.2%)
- Missing return type:= 64 (0.9%)
- Wrong return types: 7 (0.1%)
- Unaccounted: ~529 (7.8%)<= br>
All GitHub search links: https://github.com/php/php-src/pull/20398

## Question= for Community

Which approach should we take for PHP 8.6?

**O= ption A:** Accept 1-9% BC break for full type safety
**Option B:** Zero = BC breaks, add typing in PHP 9.0

I'm inclined toward Option B (z= ero breaks for 8.6), but want to hear community
preference before chang= ing the implementation.

Thoughts?

---
Additional context:<= br>- Implementation change is trivial (one line)
- Native implementation= returns array regardless of interface
- Alex's array_column suggest= ion incorporated (+3.6k usages)
- All data verifiable via GitHub se= arches in PR=C2=A0


Best regards, Mikhail


=
--00000000000064d3a80643642b5f--