3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php //////////////////////////////////////////////// // // File: abstract.WorkUnitDataType.php // //////////////////////////////////////////////// // Author: Adam Profitt // // Comments: // Work unit enumeration sim class // //////////////////////////////////////////////// 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 static $cachedConstantArr; private $value; private $name; //============================================== // construct //============================================== final public function __construct() { $name = get_class($this); $this->value = WorkUnitDataType::getValueFromName($name); $this->name = $name; } //============================================== // accessing //============================================== final public function getName() { return $this->name; } final public function getValue() { return $this->value; } //============================================== // convenience //============================================== final public static function NoneDataType() { return new NoneDataType();} final public static function BoolDataType() { return new BoolDataType();} final public static function NumericDataType(){ return new NumericDataType();} final public static function StringDataType() { return new StringDataType();} final public static function ArrayDataType() { return new ArrayDataType();} final public static function JsonDataType() { return new JsonDataType();} final public static function UrlDataType() { return new UrlDataType();} final public static function FileDataType() { return new FileDataType();} //============================================== // utility //============================================== final private static function getConstants() { if (self::$cachedConstantArr == NULL) { self::$cachedConstantArr = array(); } $calledClass = get_called_class(); if (!array_key_exists($calledClass, self::$cachedConstantArr)) { $reflect = new ReflectionClass($calledClass); self::$cachedConstantArr[$calledClass] = $reflect->getConstants(); } return self::$cachedConstantArr[$calledClass]; } final public static function getNameFromValue($const) { foreach (self::getConstants() as $name => $value) { if ($value == $const) { return $name; } } return Null; } final public static function getValueFromName($text) { foreach (self::getConstants() 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";
based on UQDKX
Output for 7.0.0 - 7.2.0
object(NoneDataType)#1 (2) { ["value":"WorkUnitDataType":private]=> int(0) ["name":"WorkUnitDataType":private]=> string(12) "NoneDataType" } Fatal error: Uncaught Error: Call to undefined method NoneDataType::toString() in /in/uuKmK:134 Stack trace: #0 {main} thrown in /in/uuKmK on line 134
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" } Fatal error: Call to undefined method NoneDataType::toString() in /in/uuKmK on line 134
Process exited with code 255.