Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check existence of childTypes method #111

Merged
merged 1 commit into from
Jul 28, 2023
Merged

Check existence of childTypes method #111

merged 1 commit into from
Jul 28, 2023

Conversation

hctorres02
Copy link
Contributor

When using enums as array keys in the properties, PHP throws a syntax error, as the interpretation is considered to be dynamic. To work around this, if the values are inside a method, they will be interpreted as part of the invocation.

The method will take precedence over the property.

Before:

protected $childTypes = [
  FooEnum::BAR->value => Bar::class,
  FooEnum::BAZ->value => Baz::class,
];

After:

protected function childTypes(): array
{
  return [
    FooEnum::BAR->value => Bar::class,
    FooEnum::BAZ->value => Baz::class,
  ];
}

@driftingly
Copy link
Member

It looks like this issue is fixed in PHP 8.2
https://wiki.php.net/rfc/fetch_property_in_const_expressions

I'll merge in for supporting earlier PHP versions as well as provide flexibility beyond Enums.

@driftingly driftingly merged commit 531253b into tighten:main Jul 28, 2023
1 check passed
@driftingly driftingly mentioned this pull request Jul 28, 2023
@driftingly
Copy link
Member

Thanks @hctorres02 🙌

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants