Skip to content

Changes to Attribute::class constants in PHP 8.5 #537

Closed as not planned
Closed as not planned

Description

@kubawerlos

The following code:

var_export((new ReflectionClass(Attribute::class))->getConstants());

In PHP 8.0-8.4 returns:

array (
  'TARGET_CLASS' => 1,
  'TARGET_FUNCTION' => 2,
  'TARGET_METHOD' => 4,
  'TARGET_PROPERTY' => 8,
  'TARGET_CLASS_CONSTANT' => 16,
  'TARGET_PARAMETER' => 32,
  'TARGET_ALL' => 63,
  'IS_REPEATABLE' => 64,
)

PHP 8.5 introduced attributes on constants, so the array in PHP 8.5 is:

array (
  'TARGET_CLASS' => 1,
  'TARGET_FUNCTION' => 2,
  'TARGET_METHOD' => 4,
  'TARGET_PROPERTY' => 8,
  'TARGET_CLASS_CONSTANT' => 16,
  'TARGET_PARAMETER' => 32,
  'TARGET_CONSTANT' => 64,
  'TARGET_ALL' => 127,
  'IS_REPEATABLE' => 128,
)

How to address the issue?

Should the existing stub be updated with something like:

    public const TARGET_CONSTANT = \PHP_VERSION_ID >= 80500 ? 64 : null;
    public const TARGET_ALL = \PHP_VERSION_ID >= 80500 ? 127 : 63;
    public const IS_REPEATABLE = \PHP_VERSION_ID >= 80500 ? 128 : 64;

Or maybe a new Attribute class in src/Php85/Resources/stubs?

There is no ternary operator in any constant yet, and also there are not 2 the same classes for different PHP versions, so each of the above would be the first such solution

Or any better idea to address the problem?

Activity

  1. nicolas-grekas commented on Jul 29, 2025

    @nicolas-grekas
    Member

    I'd go with the 8.5 values. Can you please send a PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions