3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php abstract class WorkUnitDataType { //============================================== // consts / vars //============================================== const NoneDataType = 0; const BoolDataType = 1; const StringDataType = 2; const ArrayDataType = 3; const JsonDataType = 4; const UrlDataType = 5; const FileDataType = 6; private $value; private $name; //============================================== // construct //============================================== protected function __construct() { $this->setByName(get_class($this)); } protected function setByValue($value) { if (is_int($value)) { $this->value = $value; $this->name = WorkUnitDataType::getNameFromValue($value); } } protected function setByName($name) { if (is_string($name)) { $this->value = WorkUnitDataType::getValueFromName($name); $this->name = $name; } } //============================================== // accessing //============================================== public function toString() { return $this->name; } public function toValue() { return $this->value; } //============================================== // convenience //============================================== public static function NoneDataType() { return new NoneDataType();} public static function BoolDataType() { return new BoolDataType();} public static function StringDataType() { return new StringDataType();} public static function ArrayDataType() { return new ArrayDataType();} public static function JsonDataType() { return new JsonDataType();} public static function UrlDataType() { return new UrlDataType();} public static function FileDataType() { return new FileDataType();} //============================================== // utility //============================================== public static function getNameFromValue($const) { $class = new ReflectionClass('WorkUnitDataType'); $constants = $class->getConstants(); foreach ($constants as $name => $value) { if ($value == $const) { return $name; } } return Null; } public static function getValueFromName($text) { $class = new ReflectionClass('WorkUnitDataType'); $constants = $class->getConstants(); foreach ($constants as $name => $value) { if ($name == $text) { return $value; } } return -1; } } class NoneDataType extends WorkUnitDataType {} class BoolDataType extends WorkUnitDataType {} class StringDataType extends WorkUnitDataType {} class ArrayDataType extends WorkUnitDataType {} class JsonDataType extends WorkUnitDataType {} class UrlDataType extends WorkUnitDataType {} class FileDataType extends WorkUnitDataType {} $a = WorkUnitDataType::NoneDataType(); var_dump($a); echo $a->toString() . "\n"; echo $a_>toValue() . "\n";
based on FjoJe
Output for 7.0.0 - 7.2.0
object(NoneDataType)#1 (2) { ["value":"WorkUnitDataType":private]=> int(0) ["name":"WorkUnitDataType":private]=> string(12) "NoneDataType" } NoneDataType Fatal error: Uncaught Error: Call to undefined function toValue() in /in/5G0ea:117 Stack trace: #0 {main} thrown in /in/5G0ea on line 117
Process exited with code 255.
Output for 5.5.0 - 5.6.28
object(NoneDataType)#1 (2) { ["value":"WorkUnitDataType":private]=> int(0) ["name":"WorkUnitDataType":private]=> string(12) "NoneDataType" } NoneDataType Fatal error: Call to undefined function toValue() in /in/5G0ea on line 117
Process exited with code 255.