3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php abstract class Hash { protected $algo; private $bytes; protected $data; public function __construct(string $data) { $this->bytes = strlen(hash($this->algo, '', true)); $this->data = $data; } public function getRaw() : string { return $this->value; } public function getHex() : string { return bin2hex($this->value); } public function getBase64() : string { return base64_encode($this->value); } public static function fromHex(string $data) : Hash { return new static(hex2bin($data)); } public static function fromBase64(string $data) : Hash { return new static(base64_decode($data)); } public static function getClass(string $algo) : string { $prefix = ucfirst(strtolower($algo)); $class = implode('\\', [__NAMESPACE__, "${prefix}Hash"]); return $class; } } final class Md5Hash extends Hash { protected $algo = 'md5'; } foreach(explode('|', 'md5|sha256|sha512') as $a){ $submitted[$a] = hash($a, 'foobar'); } foreach($submitted as $algo => $hexVal){ $hashes[] = (Hash::getClass($algo))::fromHex($hexVal); } var_dump($hashes);
Output for 7.0.0 - 7.3.0rc3
Fatal error: Uncaught Error: Class '\Sha256Hash' not found in /in/o2AhT:59 Stack trace: #0 {main} thrown in /in/o2AhT on line 59
Process exited with code 255.
Output for hhvm-3.22.0
Fatal error: Uncaught Error: syntax error, unexpected T_DOUBLE_COLON in /in/o2AhT:-1 Stack trace: #0 {main}
Process exited with code 255.
Output for hhvm-3.15.4 - 3.21.3
Fatal error: Uncaught Error: syntax error, unexpected T_DOUBLE_COLON in /in/o2AhT:59 Stack trace: #0 {main}
Process exited with code 255.
Output for 5.6.30
Parse error: syntax error, unexpected ':', expecting ';' or '{' in /in/o2AhT on line 16
Process exited with code 255.