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->data; } public function getHex() : string { return bin2hex($this->data); } public function getBase64() : string { return base64_encode($this->data); } 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'; } final class Sha256Hash extends Hash { protected $algo = 'sha256'; } final class Sha512Hash extends Hash { protected $algo = 'sha512'; } 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);
based on AJqOl
Output for 7.0.0 - 7.2.6
array(3) { [0]=> object(Md5Hash)#1 (3) { ["algo":protected]=> string(3) "md5" ["bytes":"Hash":private]=> int(16) ["data":protected]=> string(16) "8X�"0�<�_0 fC�?" } [1]=> object(Sha256Hash)#2 (3) { ["algo":protected]=> string(6) "sha256" ["bytes":"Hash":private]=> int(32) ["data":protected]=> string(32) "ë��7 譐G�9Fk<�t��8=J9`qL����" } [2]=> object(Sha512Hash)#3 (3) { ["algo":protected]=> string(6) "sha512" ["bytes":"Hash":private]=> int(64) ["data":protected]=> string(64) " P&�9�+�&�g<U��4-R2�=3�aj�i�X|�c_i%�l60��'5�>�$%" } }
Output for hhvm-3.15.4
Fatal error: Uncaught Error: syntax error, unexpected T_DOUBLE_COLON in /in/bSBD0:58 Stack trace: #0 {main}
Process exited with code 255.
Output for 5.6.0 - 5.6.30
Parse error: syntax error, unexpected ':', expecting ';' or '{' in /in/bSBD0 on line 16
Process exited with code 255.