Skip to content

Instantly share code, notes, and snippets.

@ramiror
Created June 21, 2020 23:33
Show Gist options
  • Save ramiror/f81ae366c3da3e77fd0d610a0f9ef29d to your computer and use it in GitHub Desktop.
Save ramiror/f81ae366c3da3e77fd0d610a0f9ef29d to your computer and use it in GitHub Desktop.
A simpler enumish class
<?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