Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:129107 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 7E8BA1A00BC for ; Thu, 6 Nov 2025 04:09:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1762402177; bh=qCyt1sAp3G4dHVW9ghvqpXdUfGl1UM416I4Scz95V3w=; h=From:Date:Subject:To:From; b=RbxaDa6PHbtbzZTeB9hKqTz3K/amLw48UCrCbKzvQ4b+15H161oLX5eaX+rZqmoWA jlrnzdj2C02eby/qa1kLLJfYlE04JdiFq2jO9VEephBJQvxjOgF0A+LhBh8GjLH/Xr 6htx/PK0sBRaj+cKymOqG6CNdpwRngBSs+5Ar6tMuLybKsWHLfD+aPKM08+0OmfHzB TYKykGz3S381siKbzjNuYNVw/wMQ1cy4esxbP0Y0wOrcnwTGgPr4L2qzz8KKKRMZpv ZFI8jCdg4Quvy2D5jzARuWhuUl9V+XzuI4dqS8TdPwc6NF1k3IW0XpzqXlp30GF/yx oLwhrQKEls7iw== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 27B2D18006E for ; Thu, 6 Nov 2025 04:09:36 +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_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-f42.google.com (mail-ej1-f42.google.com [209.85.218.42]) (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 ; Thu, 6 Nov 2025 04:09:35 +0000 (UTC) Received: by mail-ej1-f42.google.com with SMTP id a640c23a62f3a-b70bee93dc4so66020466b.3 for ; Wed, 05 Nov 2025 20:09:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1762402169; x=1763006969; darn=lists.php.net; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=zByThcMkoZjazLmfnG3jh/qbnPYazgV0qsE7tB9JyTo=; b=D1TTs0qTHv7UDps+WWlsGDsB1K4O+TTJzQJjIqVk0SHyammE9rSmGUEdZKDLwUehEx eY6ES7waY85qlpSRf47Do0ovconNL1VX5r0EdaCVFSLRKiL/gX5nlpRBqC0qkP5JYKAe MumsEjeqUX5UkYeCrguiH+9QJROQHGjlPzkua8YJeS0oykagPyz/naAzh+5dpMVQ8rhK o/2yJARTknmiXRm2SC++JE15h+w0nCXkpwkOU+y3tnZEG8RtFv3fp1QwWhlr+gQPYR1S IDRRFqBBm+KeBndY1PeqKjyIRtcAMg4ee3qfmKfYe6EbXwqRsbAjvT2nHur9IKSOBx8G PeCA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1762402169; x=1763006969; h=to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=zByThcMkoZjazLmfnG3jh/qbnPYazgV0qsE7tB9JyTo=; b=G/CVK7bQLW1H/wsiWfo0ApUyYAYrOBznxpMgoJPl2pDgNo2vmIvlEmsm679G/ZxjPo ZY1cU+N6Lb9kf3CnX2vjmWLNUrDjUgS0TUhpINUjJ2R8rnd8vToLZV34ru11YwPEvb1j ZxTfuZXxgd5PKfv4Oq2qIX+RfG8cvWAGS/XoPR2lFm7VD4TGDMlFhwo5dkKGga/VJDFG RhWSbX1Y8u8DkjsNloKpkLPUOY1dvkekopIifrtm4mFsBznj3zuMnElMsgMP1F99TFaP SC+ZVJqAOIg8hDKw7g97qhFVEXHO9eRU9ieTiuOEHxiU4ya+aHl94LGTvVTQLY8m2UYN 8LQQ== X-Gm-Message-State: AOJu0YzOT5+rmFZ0P3WXnPozGHV1woo/FjUGNXqYEAh1hxxS+Hu2ce3M sXJGSAEKgArgo31JmSTNJoP17QkKhMOGBE0g0SfrAIR23n6VA9PHtONOKtZ+71fWFNGD/X8bz5A 06xrQxBku/DrGo+NQR8tMk4pzusAKRC7m2CgXZOM= X-Gm-Gg: ASbGncsh3pVJT4ImCB3yKcYWOBGwu+cE1DN6BJ0MX2PJGVaEMCFvhB5t+pEhLFU/y2u ZMB5/J+A2RaR5EXQsJB4SCOU4AgNOPdSDCIUdr+x/cITKlQFqTkfQiZ4C438In9fAqr+gFiCU96 RYuxNndQqFw4HFFATTNqml+xOM148lZQKzxxrWJk97lH+xKYW9Pj9FCmiMAkNpqciYmtfydHssx vY7v0XXxiuzYNHipRIUXHO68tZ7UVD6ZxsYnqW9QBzObSEQNr+GzQhKbqzueAngd/QRpwzFBwtG dQsMiytKGL9FNmqAuw== X-Google-Smtp-Source: AGHT+IFtqwdiKot2mZ8QYG3VZNMlmfw2frK7yAAdvrw1sHEy9lxV9C+qRdoXsbkhhWsqLUC1G7R2eQZthbBDZe5kMdI= X-Received: by 2002:a17:907:d649:b0:b70:e15b:2883 with SMTP id a640c23a62f3a-b7265649d11mr537306766b.58.1762402169234; Wed, 05 Nov 2025 20:09:29 -0800 (PST) Precedence: list list-help: list-unsubscribe: list-post: List-Id: x-ms-reactions: disallow MIME-Version: 1.0 Date: Thu, 6 Nov 2025 07:09:18 +0300 X-Gm-Features: AWmQ_bmxTMpaECwrrTpGF5VN2MF53brfsfioQDe_Qqi19uYOdwKavq2SB0CelOA Message-ID: Subject: [PHP-DEV] [RFC] [Discussion] BackedEnum::values() - Seeking feedback before formal RFC To: internals@lists.php.net Content-Type: multipart/alternative; boundary="0000000000003fa80c0642e53b42" From: mikhail.d.savin@gmail.com (Mikhail Savin) --0000000000003fa80c0642e53b42 Content-Type: text/plain; charset="UTF-8" Hi internals, I would like to propose adding a native values() method to the BackedEnum interface that returns an array of all backing values. Before creating a formal RFC, I'm seeking feedback on the concept and approach. == Summary == The proposal adds: interface BackedEnum { public static function values(): array; } This would allow: enum Status: string { case Active = 'active'; case Inactive = 'inactive'; } Status::values(); // ['active', 'inactive'] == Motivation == This pattern is extremely common in the wild. Based on GitHub code search: * ~3,860+ direct implementations of this exact pattern * ~20,000-40,000 estimated real usage when accounting for shared traits * Used in major frameworks: Symfony core (TypeIdentifier.php), Laravel ecosystem * Documented by PHP.net: The manual itself shows EnumValuesTrait as an example Common use cases: * Database migrations: $table->enum('status', Status::values()) * Form validation: $validator->rule('status', 'in', Status::values()) * API responses: ['allowed_statuses' => Status::values()] == Implementation == I have a working implementation with tests: https://github.com/php/php-src/pull/20398 The implementation: * Mirrors the existing cases() method structure * Extracts the value property from each case * Returns an indexed array (0, 1, 2, ...) * Only available on BackedEnum, not UnitEnum * All tests pass == Backward Compatibility - Important Discussion Point == This is a breaking change. Enums that already define a values() method will fail with: Fatal error: Cannot redeclare BackedEnum::values() Based on ecosystem research: * ~24,000-44,000 enum instances will break * All implementations are functionally identical to what's being proposed * Migration is mechanical: just delete the user-defined method The break is justified because: 1. Behavior is unchanged - native implementation does exactly what users already implemented 2. Migration is trivial - simply remove the redundant method 3. Precedent exists - PHP 8.1 native enums broke myclabs/php-enum (4.9k stars) similarly 4. Long-term benefit - standardization, discoverability, elimination of boilerplate 5. No alternative - virtual properties are technically infeasible; different name doesn't match community expectations == Questions for Discussion == 1. BC break acceptability: Given the scope and straightforward migration, is this break acceptable? 2. Method name: values() matches community usage (3,860+ examples) and parallels cases(). Alternatives like getValues() or toArray() were considered but seem inferior. Thoughts? 3. Target version: Currently targeting PHP 8.6 (master branch). Is this appropriate? 4. Deprecation period: Should we emit E_DEPRECATED in 8.5 and fatal error in 9.0? Or accept the break immediately? (Deprecation adds engine complexity and delays benefit.) == Prior Art == * Symfony: Uses this pattern in core components * PHP.net Manual: Documents EnumValuesTrait approach * TypeScript: Object.values(Enum) * Python: [e.value for e in Enum] * myclabs/php-enum: Had values() method (4.9k stars) == Next Steps == If feedback is generally positive, I will: 1. Request RFC karma 2. Create formal RFC on wiki.php.net 3. Address any concerns raised in this discussion 4. Move to formal voting after discussion period == Implementation Details == For those interested in the technical details, the PR includes: * Core implementation in zend_enum.c * Stub file updates * Comprehensive test coverage (9 test files) * Reflection support * Documentation in NEWS and UPGRADING PR: https://github.com/php/php-src/pull/20398 Looking forward to your feedback! Best regards, Savin Mikhail GitHub: @savinmikhail --0000000000003fa80c0642e53b42 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Hi internals,

I would like to propose adding a nati= ve values() method to the BackedEnum
interface that returns an array of = all backing values. Before creating a
formal RFC, I'm seeking feedba= ck on the concept and approach.

=3D=3D Summary =3D=3D

The pro= posal adds:

=C2=A0 =C2=A0 interface BackedEnum {
=C2=A0 =C2=A0 = =C2=A0 =C2=A0 public static function values(): array;
=C2=A0 =C2=A0 }
This would allow:

=C2=A0 =C2=A0 enum Status: string {
=C2=A0= =C2=A0 =C2=A0 =C2=A0 case Active =3D 'active';
=C2=A0 =C2=A0 = =C2=A0 =C2=A0 case Inactive =3D 'inactive';
=C2=A0 =C2=A0 }
= =C2=A0 =C2=A0
=C2=A0 =C2=A0 Status::values(); // ['active', '= ;inactive']

=3D=3D Motivation =3D=3D

This pattern is extr= emely common in the wild. Based on GitHub code search:

=C2=A0 * ~3,8= 60+ direct implementations of this exact pattern
=C2=A0 * ~20,000-40,000= estimated real usage when accounting for shared traits
=C2=A0 * Used in= major frameworks: Symfony core (TypeIdentifier.php),
=C2=A0 =C2=A0 Lara= vel ecosystem
=C2=A0 * Documented by PHP.net: The manual itself shows En= umValuesTrait as
=C2=A0 =C2=A0 an example

Common use cases:
= =C2=A0 * Database migrations: $table->enum('status', Status::val= ues())
=C2=A0 * Form validation: $validator->rule('status', &= #39;in', Status::values())
=C2=A0 * API responses: ['allowed_sta= tuses' =3D> Status::values()]

=3D=3D Implementation =3D=3D
I have a working implementation with tests:
https://github.com/php/php= -src/pull/20398

The implementation:
=C2=A0 * Mirrors the exis= ting cases() method structure
=C2=A0 * Extracts the value property from = each case
=C2=A0 * Returns an indexed array (0, 1, 2, ...)
=C2=A0 * O= nly available on BackedEnum, not UnitEnum
=C2=A0 * All tests pass
=3D=3D Backward Compatibility - Important Discussion Point =3D=3D

T= his is a breaking change. Enums that already define a values() method
wi= ll fail with:

=C2=A0 =C2=A0 Fatal error: Cannot redeclare BackedEnum= ::values()

Based on ecosystem research:
=C2=A0 * ~24,000-44,000 e= num instances will break
=C2=A0 * All implementations are functionally i= dentical to what's being proposed
=C2=A0 * Migration is mechanical: = just delete the user-defined method

The break is justified because:<= br>
=C2=A0 1. Behavior is unchanged - native implementation does exactly= what users
=C2=A0 =C2=A0 =C2=A0already implemented
=C2=A0 2. Migrati= on is trivial - simply remove the redundant method
=C2=A0 3. Precedent e= xists - PHP 8.1 native enums broke myclabs/php-enum
=C2=A0 =C2=A0 =C2=A0= (4.9k stars) similarly
=C2=A0 4. Long-term benefit - standardization, di= scoverability, elimination
=C2=A0 =C2=A0 =C2=A0of boilerplate
=C2=A0 = 5. No alternative - virtual properties are technically infeasible;
=C2= =A0 =C2=A0 =C2=A0different name doesn't match community expectations
=3D=3D Questions for Discussion =3D=3D

1. BC break acceptabilit= y: Given the scope and straightforward migration,
=C2=A0 =C2=A0is this b= reak acceptable?

2. Method name: values() matches community usage (3= ,860+ examples) and
=C2=A0 =C2=A0parallels cases(). Alternatives like ge= tValues() or toArray() were
=C2=A0 =C2=A0considered but seem inferior. T= houghts?

3. Target version: Currently targeting PHP 8.6 (master bran= ch). Is this
=C2=A0 =C2=A0appropriate?

4. Deprecation period: Sho= uld we emit E_DEPRECATED in 8.5 and fatal error
=C2=A0 =C2=A0in 9.0? Or = accept the break immediately? (Deprecation adds engine
=C2=A0 =C2=A0comp= lexity and delays benefit.)

=3D=3D Prior Art =3D=3D

=C2=A0 * = Symfony: Uses this pattern in core components
=C2=A0 * PHP.net Manual: D= ocuments EnumValuesTrait approach
=C2=A0 * TypeScript: Object.values(Enu= m)
=C2=A0 * Python: [e.value for e in Enum]
=C2=A0 * myclabs/php-enum= : Had values() method (4.9k stars)

=3D=3D Next Steps =3D=3D

I= f feedback is generally positive, I will:
=C2=A0 1. Request RFC karma=C2=A0 2. Create formal RFC on=C2=A0wiki.php.net
=C2=A0 3. Address any concerns raised in t= his discussion
=C2=A0 4. Move to formal voting after discussion period
=3D=3D Implementation Details =3D=3D

For those interested in t= he technical details, the PR includes:
=C2=A0 * Core implementation in z= end_enum.c
=C2=A0 * Stub file updates
=C2=A0 * Comprehensive test cov= erage (9 test files)
=C2=A0 * Reflection support
=C2=A0 * Documentati= on in NEWS and UPGRADING

PR:=C2=A0https://github.com/php/php-src/pull/2= 0398

Looking forward to your feedback!

Best regards, Sav= in Mikhail
GitHub: @savinmikhail
--0000000000003fa80c0642e53b42--