dbalabka / php-enumeration

Implementation of enumeration classes in PHP. The better alternative for enums
MIT License
53 stars 4 forks source link

Autoloader bug when used with Doctrine and Annotations. #25

Open Warxcell opened 3 years ago

Warxcell commented 3 years ago

Doctrine is using manually require_once and if it happens Enum class to be in Entity folder - It bypasses the autoloader and __constructStatic is never called.

https://github.com/doctrine/persistence/blob/2.1.x/lib/Doctrine/Persistence/Mapping/Driver/AnnotationDriver.php#L234

I don't have idea how to fix it generally. For quick fix in my code i did intermediate class.

final class EntityEnum extends NonEntityEnum
{
}
abstract class NonEntityEnum extends Enumeration
{
    public static EntityEnum $value;

    protected static function initializeValues(): void
    {
        self::$value = new EntityEnum();
    }
}

so when Doctrine require_once 'EntityEnum.php'; it will lead autoloader to NonEntityEnum and it will call the constructStatic.

Warxcell commented 3 years ago

I discovered that this method usually break up randomly. so I ended up like that:

EntityEnum.php

<?php

declare(strict_types=1);

class_alias(NonEntityEnm::class, 'EntityEnum');
dbalabka commented 3 years ago

@Warxcell thanks for the bug report. I will find out how to solve it for you.

In general, I'm going to suspend the whole development of this repository but will keep updates for people who already using it. I've mentioned it here as well: https://github.com/dbalabka/php-enumeration/issues/17#issuecomment-781670458

Warxcell commented 3 years ago

I doubt it there is a fix for that. At least I couldn't figure it out. Just posted here to be known for everybody. :)