tighten / parental

Use single table inheritance in your Laravel app
MIT License
1.36k stars 98 forks source link

Check existence of childTypes method #111

Closed hctorres02 closed 1 year ago

hctorres02 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

Thanks @hctorres02 🙌