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 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();} final public static function equals($class) { if( is_subclass_of($class, 'WorkUnitDataType')) { return $this->getValue() == $class->getValue(); } return False; } //============================================== // 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 isValidName($name, $strict = false) { $constants = self::getConstants(); if ($strict) { return array_key_exists($name, $constants); } $keys = array_map('strtolower', array_keys($constants)); return in_array(strtolower($name), $keys); } final public static function isValidValue($value) { $values = array_values(self::getConstants()); return in_array($value, $values, $strict = true); } 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::NumericDataType(); var_dump($a); echo "\n"; echo $a->getName() . "\n"; echo $a->getValue() . "\n"; echo "\n\n"; $a = WorkUnitDataType::BoolDataType(); var_dump($a); echo "\n"; echo $a->getName() . "\n"; echo $a->getValue() . "\n"; echo "\n\n"; echo $a->equals(WorkUnitDataType::NumericDataType()) . "\n"; echo $a->equals(WorkUnitDataType::BoolDataType()) . "\n";
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
Warning: Private methods cannot be final as they are never overridden by other classes in /in/FMum1 on line 74 object(NumericDataType)#1 (2) { ["value":"WorkUnitDataType":private]=> int(2) ["name":"WorkUnitDataType":private]=> string(15) "NumericDataType" } NumericDataType 2 object(BoolDataType)#2 (2) { ["value":"WorkUnitDataType":private]=> int(1) ["name":"WorkUnitDataType":private]=> string(12) "BoolDataType" } BoolDataType 1 Fatal error: Uncaught Error: Using $this when not in object context in /in/FMum1:63 Stack trace: #0 /in/FMum1(157): WorkUnitDataType::equals(Object(NumericDataType)) #1 {main} thrown in /in/FMum1 on line 63
Process exited with code 255.
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 Warning: Private methods cannot be final as they are never overridden by other classes in /in/FMum1 on line 74 object(NumericDataType)#1 (2) { ["value":"WorkUnitDataType":private]=> int(2) ["name":"WorkUnitDataType":private]=> string(15) "NumericDataType" } NumericDataType 2 object(BoolDataType)#2 (2) { ["value":"WorkUnitDataType":private]=> int(1) ["name":"WorkUnitDataType":private]=> string(12) "BoolDataType" } BoolDataType 1 Fatal error: Uncaught Error: Using $this when not in object context in /in/FMum1:63 Stack trace: #0 /in/FMum1(157): WorkUnitDataType::equals(Object(NumericDataType)) #1 {main} thrown in /in/FMum1 on line 63
Process exited with code 255.
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
object(NumericDataType)#1 (2) { ["value":"WorkUnitDataType":private]=> int(2) ["name":"WorkUnitDataType":private]=> string(15) "NumericDataType" } NumericDataType 2 object(BoolDataType)#2 (2) { ["value":"WorkUnitDataType":private]=> int(1) ["name":"WorkUnitDataType":private]=> string(12) "BoolDataType" } BoolDataType 1 Fatal error: Uncaught Error: Using $this when not in object context in /in/FMum1:63 Stack trace: #0 /in/FMum1(157): WorkUnitDataType::equals(Object(NumericDataType)) #1 {main} thrown in /in/FMum1 on line 63
Process exited with code 255.
Output for 5.5.0 - 5.5.38, 5.6.0 - 5.6.28
object(NumericDataType)#1 (2) { ["value":"WorkUnitDataType":private]=> int(2) ["name":"WorkUnitDataType":private]=> string(15) "NumericDataType" } NumericDataType 2 object(BoolDataType)#2 (2) { ["value":"WorkUnitDataType":private]=> int(1) ["name":"WorkUnitDataType":private]=> string(12) "BoolDataType" } BoolDataType 1 Fatal error: Using $this when not in object context in /in/FMum1 on line 63
Process exited with code 255.

preferences:
191.04 ms | 402 KiB | 225 Q