Skip to content

AllowDynamicProperties validation should error on enums #15731

Closed

Description

@DanielEScherzer

Description

The following code:

<?php

#[AllowDynamicProperties]
enum Example {
    case MyCase;
}

$c = Example::MyCase;
$c->prop = 'foo';

Resulted in this output:

Fatal error: Uncaught Error: Cannot create dynamic property Example::$prop in /in/Aqipu:9
Stack trace:
#0 {main}
  thrown in /in/Aqipu on line 9

Process exited with code 255.

But I expected this output instead:

Fatal error: Cannot apply #[AllowDynamicProperties] to enum in /in/Aqipu on line 4

Process exited with code 255.

PHP Version

PHP 8.2

Operating System

No response

Activity

  1. DanielEScherzer commented on Sep 3, 2024

    @DanielEScherzer
    MemberAuthor

    See 3v4l https://3v4l.org/Aqipu - if enumerations can never have dynamic properties (as per https://www.php.net/manual/en/language.enumerations.object-differences.php) then we should prevent the dynamic properties attribute from being applied, since its presence generally indicates that it is safe to use dynamic properties

  2. devnexen commented on Sep 3, 2024

    @devnexen
    Member

    seems fair point (and easy enough to fix) @iluuu1994 your thoughts ?

  3. kocsismate commented on Sep 4, 2024

    @kocsismate
    Member

    I've already shared my POV in the relevant PR:

    The change looks good on its own but I'm wondering whether it isn't an oversight of the enum RFC that attributes targeting classes are allowed for enums? It looks like to me that we should(n't have) allow(ed) this, so the best course of action would be to add a new target for enums, and only allow the usage if it's set.

    #15733 (comment)

  4. added a commit that references this issue on Sep 4, 2024
    5a18279
  5. DanielEScherzer commented on Sep 4, 2024

    @DanielEScherzer
    MemberAuthor

    @devnexen should this be added to NEWS or UPGRADING since previously the attribute would be allowed, just not useful?

  6. devnexen commented on Sep 4, 2024

    @devnexen
    Member

    can you make a PR ? I ll merge it.

  7. added a commit that references this issue on Sep 4, 2024
  8. DanielEScherzer commented on Sep 4, 2024

    @DanielEScherzer
    MemberAuthor

    Sent #15756 - wasn't sure if this is needed for UPGRADING since the error messages will be very clear, and since it hasn't been reported by anyone else unlikely to affect people, so I just put it in NEWS

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions