PHP code example of froler314 / enum

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

    

froler314 / enum example snippets


class MyEnum extends Enum {
    const CONSTANT_A = 'value_a';
    const CONSTANT_B = 'value_b';
}

$enumObject = MyEnum::getInstance(MyEnum::CONSTANT_A);
echo $enumObject->getValue(); // prints 'value_a'
echo $enumObject;             // also prints 'value_a'

function someFunction(MyEnum $enumObject): void
{
    // ...
}

$enumObject = MyEnum::getInstance(MyEnum::CONSTANT_A);
someFunction($enumObject);

function compareFunction(MyEnum $firstEnumObject, MyEnum $secondEnumObject): bool
{
    return $firstEnumObject === $secondEnumObject       // both variants
        || $firstEnumObject->equals($secondEnumObject)  // are identical
}

/**
 * @method static self valueA()
 * @method static self valueB()
 */
class MyEnum extends Enum {
    use MagicStaticCallEnum; // use MagicStaticCallEnum trait

    const CONSTANT_A = 'value_a';
    const CONSTANT_B = 'value_b';
}

$enumObject = MyEnum::valueA(); // instead of MyEnum::getInstance(MyEnum::CONSTANT_A)