PHP code example of lip / php-enum

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

    

lip / php-enum example snippets



use lip\enum\Enum;

/**
 * 一个很有意思的枚举。
 * @method static self One()    One的函数说明
 * @method static self Two()    Two的函数说明
 * @method static self Three()  Three的函数说明
 */
final class Some extends Enum
{
    private const One = [1, '一'];
    private const Two = [2, '二'];
    private const Three = [3, '三'];
}

function useSome(Some $some) {
    // ...
}

useSome(Some::One());

function useSome(Some $some)
{
    $key = $some->key();
    $value = $some->value();
    $label => $some->label();
    // $some 可以直接参与字符串连接运算,相当于 $some->value() 参与了运算,例如:
    echo 'some value is: ' . $some;
    // 相当于
    echo 'some value is: ' . $some->value();
}

/**
 * 另一个很有意思的枚举。
 * @method static self Haha() 哈哈大笑
 * @method static self Bibi() You can you up, no can no bibi
 */
final class Other extends Enum
{
    private const Haha = 'hh';
    private const Bibi = 'bb';
}

// Other 实例的 value 与 label 是相同的,都是 hh
Other::Haha()->value() == Other::Haha()->label()   // true

Some::One() === Some::One()  // true
Some::One() !== Some::One()  // false

if ($some === Some::One()) {
    // ...
} else if ($some === Some::Two()) {
    // ...
} else if ($some === Some::Three()) {
    // ...
} else {
    // ...
}
// 或者
switch ($some) {
    case Some::One():
        // ...
        break;
    case Some::Two():
        // ...
        break;
    case Some::Three():
        // ...
        break;
    default:
        // ...
        break;
}

json_encode(Some::One()); // '{"key":"One","value":1,"label":"\u4e00"}'
bash
composer