Created
June 21, 2020 23:33
-
-
Save ramiror/f81ae366c3da3e77fd0d610a0f9ef29d to your computer and use it in GitHub Desktop.
A simpler enumish class
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 | |
abstract class Pais { | |
const ARGENTINA = 'Argentina'; | |
const URUGUAY = 'Uruguay'; | |
static private $values = []; | |
static function init() { | |
$refle = new ReflectionClass(__CLASS__); | |
foreach ($refle->getConstants() as $symbol => $con) { | |
echo "encontre una constante: $symbol -> $con\n"; | |
self::$values[$con] = new $con; | |
} | |
} | |
static function get($symbol) { | |
return self::$values[$symbol]; | |
} | |
abstract function getUrl(); | |
function __toString() { | |
return get_called_class(); | |
} | |
} | |
class Argentina extends Pais { | |
function getUrl() { return 'AAA'; } | |
} | |
class Uruguay extends Pais { | |
function getUrl() { return 'UUU'; } | |
} | |
Pais::init(); | |
echo "ar constant: " . Pais::ARGENTINA . PHP_EOL; | |
echo "ar object : " . Pais::get(Pais::ARGENTINA) . PHP_EOL; | |
echo "ar url : " . Pais::get(Pais::ARGENTINA)->getUrl() . PHP_EOL; | |
echo "uy constant: " . Pais::URUGUAY . PHP_EOL; | |
echo "uy object : " . Pais::get(Pais::URUGUAY) . PHP_EOL; | |
echo "uy url : " . Pais::get(Pais::URUGUAY)->getUrl() . PHP_EOL; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment