Skip to content

Instantly share code, notes, and snippets.

@do-aki
Last active February 28, 2018 15:03
Show Gist options
  • Save do-aki/eee23303ea3d1c1cd2352ec4c7ba0479 to your computer and use it in GitHub Desktop.
Save do-aki/eee23303ea3d1c1cd2352ec4c7ba0479 to your computer and use it in GitHub Desktop.
EnumTrait usage
<?php
final class GameDifficulty
{
const HARD = 'hard';
const NORMAL = 'normal';
const EASY = 'easy';
use EnumTrait {
constant as public HARD;
constant as public NORMAL;
constant as public EASY;
}
}
final class CheeseType
{
const HARD = 'hard';
const SEMIHARD = 'semihard';
const FRESH = 'fresh';
use EnumTrait {
constant as public HARD;
constant as public SEMIHARD;
constant as public FRESH;
}
}
$difficulty = GameDifficulty::HARD();
var_dump($difficulty === GameDifficulty::HARD()); // bool(true)
var_dump($difficulty === CheeseType::HARD()); // bool(false)
var_dump($difficulty->is(GameDifficulty::HARD())); // bool(true)
var_dump($difficulty->is(CheeseType::HARD())); // throw TypeError
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment