PHP code example of prinsfrank / enums

1. Go to this page and download the library: Download prinsfrank/enums library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

prinsfrank / enums example snippets


enum Example {
    case Foo;

    #[HasSpecialMeaning]
    case Bar;
}

UnitEnum::fromName(Example::class, 'Foo'); // Example::Foo
UnitEnum::tryFromName(Example::class, 'Foo'); // Example::Foo


UnitEnum::fromName(Example::class, 'Example'); // @throws NameNotFoundException
UnitEnum::tryFromName(Example::class, 'Example'); // null

UnitEnum::names(Example::class); // ['Foo', 'Bar']

UnitEnum::hasCaseAttributes(Example::Bar, HasSpecialMeaning); // true
UnitEnum::getCaseAttributes(Example::Bar); // [new HasSpecialMeaning()]

enum Example: string {
    case Foo = 'Foo';

    #[HasSpecialMeaning]
    case Bar = 'Bar';
}

BackedEnum::fromName(Example::class, 'Foo'); // Example::Foo
BackedEnum::tryFromName(Example::class, 'Foo'); // Example::Foo


BackedEnum::fromName(Example::class, 'Example'); // @throws NameNotFoundException
BackedEnum::tryFromName(Example::class, 'Example'); // null

BackedEnum::names(Example::class); // ['Foo', 'Bar']

BackedEnum::hasCaseAttributes(Example::Bar, HasSpecialMeaning); // true
BackedEnum::getCaseAttributes(Example::Bar); // [new HasSpecialMeaning()]