3v4l.org

run code in 300+ PHP versions simultaneously
<?php abstract class WorkUnitDataType { //============================================== // consts / vars //============================================== const NoneDataType = 0; const BoolDataType = 1; const NumericDataType = 2; const StringDataType = 3; const ArrayDataType = 4; const JsonDataType = 5; const UrlDataType = 6; const FileDataType = 7; private $value; private $name; private $cachedConstantArr; //============================================== // construct //============================================== protected function __construct() { $this->setByName(get_class($this)); $this->cacheConstArray(); } //============================================== // accessing //============================================== public function getName() { return $this->name; } public function getValue() { return $this->value; } private function cacheConstArray() { $class = new ReflectionClass('WorkUnitDataType'); $this->cachedConstantArr = $class->getConstants(); } //============================================== // convenience //============================================== public static function NoneDataType() { return new NoneDataType();} public static function BoolDataType() { return new BoolDataType();} public static function NumericDataType(){ return new NumericDataType();} 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 //============================================== 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; } } public static function getNameFromValue($const) { foreach ($this->cachedConstantArr as $name => $value) { if ($value == $const) { return $name; } } return Null; } public static function getValueFromName($text) { foreach ($this->cachedConstantArr as $name => $value) { if ($name == $text) { return $value; } } return -1; } } class NoneDataType extends WorkUnitDataType {} class BoolDataType extends WorkUnitDataType {} class NumericDataType 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 "\n\n"; echo $a->toString() . "\n"; echo $a->toValue() . "\n";
Output for 7.0.0 - 7.0.20, 7.1.0 - 7.1.20, 7.2.0 - 7.2.33, 7.3.16 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
Fatal error: Uncaught Error: Using $this when not in object context in /in/uNkv9:94 Stack trace: #0 /in/uNkv9(78): WorkUnitDataType::getValueFromName('NoneDataType') #1 /in/uNkv9(28): WorkUnitDataType->setByName('NoneDataType') #2 /in/uNkv9(55): WorkUnitDataType->__construct() #3 /in/uNkv9(120): WorkUnitDataType::NoneDataType() #4 {main} thrown in /in/uNkv9 on line 94
Process exited with code 255.
Output for 5.5.0 - 5.5.38, 5.6.0 - 5.6.28
Fatal error: Using $this when not in object context in /in/uNkv9 on line 94
Process exited with code 255.

preferences:
159.84 ms | 402 KiB | 225 Q