Last active
June 3, 2023 23:36
-
-
Save the-liquid-metal/a2608f197e1241d11c956890b7c8a635 to your computer and use it in GitHub Desktop.
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
The ordinary and simplest way to write enum in PHP is: | |
class MyEnum { | |
const val1 = "val1"; | |
const val2 = "val2"; | |
const val3 = "val3"; | |
} | |
and the way to use enum is: | |
$val == MyEnum::val1; | |
But this is error prone. | |
compared to bellow, you just need to write: | |
$val == MyEnum::val1(); | |
Yes, it is verbose, but nothing is sacrificed. | |
Give it a try, and you will feel that PHP is really does not need a native enum. | |
And amazingly, it falls back to PHP 5.2. | |
try: http://sandbox.onlinephpfunctions.com/code/c69e09d7da667a6cb5823ec81d37627fbdfcb580 | |
<?php | |
abstract class Enum { | |
protected $val; | |
protected function __construct($arg) { | |
$this->val = $arg; | |
} | |
public function __toString() { | |
return $this->val; | |
} | |
public function __set($arg1, $arg2) { | |
throw new Exception("enum does not have property"); | |
} | |
public function __get($arg1) { | |
throw new Exception("enum does not have property"); | |
} | |
// not really needed | |
public function __call($arg1, $arg2) { | |
throw new Exception("enum does not have method"); | |
} | |
// not really needed | |
static public function __callStatic($arg1, $arg2) { | |
throw new Exception("enum does not have static method"); | |
} | |
} | |
final class MyEnum extends Enum { | |
static public function val1() { | |
return new self("val1"); | |
} | |
static public function val2() { | |
return new self("val2"); | |
} | |
static public function val3() { | |
return new self("val3"); | |
} | |
} | |
// -------------------------------------------- | |
$a = MyEnum::val1(); | |
echo "1.the enum value is '$a'\n"; | |
function consumeMyEnum(MyEnum $arg) { | |
return "2.the return value is '$arg'\n"; | |
} | |
echo consumeMyEnum($a); | |
$version = explode(".", PHP_VERSION); | |
if ($version[0] >= 7) { | |
try { | |
echo consumeMyEnum("val1"); | |
} catch (TypeError $e) { | |
echo "3.passing argument error happens (PHP 7.0 and above)\n"; | |
} | |
} | |
echo ($a == MyEnum::val1()) ? "4.same\n" : "4.different\n"; | |
echo ($a == MyEnum::val2()) ? "5.same\n" : "5.different\n"; | |
$b = MyEnum::val1(); | |
echo ($a == $b) ? "6.same\n" : "6.different\n"; | |
echo ($a === $b) ? "7.same\n" : "7.different\n"; | |
$c = MyEnum::val2(); | |
echo ($a == $c) ? "8.same\n" : "8.different\n"; | |
echo ($a === $c) ? "9.same\n" : "9.different\n"; | |
switch ($c) { | |
case MyEnum::val1(): echo "10.case of 1st\n"; break; | |
case MyEnum::val2(): echo "11.case of 2nd\n"; break; | |
case MyEnum::val3(): echo "12.case of 3rd\n"; break; | |
} | |
// cache it if speed and room are critical | |
$arr = array(); | |
$val1 = MyEnum::val1() . ""; | |
$val2 = MyEnum::val2() . ""; | |
$val3 = MyEnum::val3() . ""; | |
for ($i = 0; $i < 10; $i++) { | |
$arr[] = MyEnum::val3(); | |
} | |
foreach($arr as $item) { | |
switch ($item) { | |
case $val1: echo "13.cache of 1st\n"; break; | |
case $val2: echo "14.cache of 2nd\n"; break; | |
case $val3: echo "15.cache of 3rd\n"; break; | |
} | |
} | |
try { | |
$a->prop = 10; | |
} catch (Exception $e) { | |
echo "16.set property error happens\n"; | |
} | |
try { | |
echo $a->prop; | |
} catch (Exception $e) { | |
echo "17.get property error happens\n"; | |
} | |
try { | |
echo $a->meth(); | |
} catch (Exception $e) { | |
echo "18.method call error happens\n"; | |
} | |
try { | |
echo MyEnum::meth(); | |
} catch (Exception $e) { | |
echo "19.static method call error happens\n"; | |
} | |
class Ordinary {} | |
echo $a instanceof MyEnum ? "20.MyEnum instance\n" : "20.not MyEnum instance\n"; | |
echo $a instanceof Enum ? "21.Enum instance\n" : "21.not Enum instance\n"; | |
echo $a instanceof Ordinary ? "22.Ordinary instance\n" : "22.not Ordinary instance\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment