PHP code example of youngsource / typed-enum

1. Go to this page and download the library: Download youngsource/typed-enum 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/ */

    

youngsource / typed-enum example snippets


final class Foo extends TypedEnum {
    private const BAR = 'bar';
    private const BAZ = 2;
    private const FOO = 2.1;
} 

Foo::BAR();  // Returns an instance of Foo with value 'bar'

function isBar(Foo $enum): bool {
    return Foo::BAR() === $enum;     
}

echo Foo::BAR()->getValue(); //'bar'

echo Foo::resolve('bar')[0] === Foo::BAR() // true 

/**
 * @method static TypedEnum TEST()
 */
final class Foo extends TypedEnum {
    private const TEST = 'test';
}

/**
 * @extends TypedEnum<string>
 */
final class Foo extends TypedEnum {
    private const TEST = 'test';
}

echo Foo::BAR === Foo::BAR() // echo's false but is impossible if it is a protected constant