Last active
February 28, 2018 15:03
-
-
Save do-aki/eee23303ea3d1c1cd2352ec4c7ba0479 to your computer and use it in GitHub Desktop.
EnumTrait usage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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