Skip to content

Variable $this is undefined in enum #288

Closed
@satiromarra

Description

@satiromarra

When a function is defined inside an enum, the use of $this appears as an undefined variable.

enum Numbers: string {
  case ONE   = '1';
  case TWO   = '2';
  case THREE = '3';
  case FOUR  = '4';

  public function divisibility(): string {
    return match ($this) { // <--- Variable $this is undefined
      self::ONE, self::THREE => 'odd',
      self::TWO, self::FOUR => 'even',
    };
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions