3v4l.org

run code in 300+ PHP versions simultaneously
<?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'; } 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);
Output for 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
Deprecated: Using ${var} in strings is deprecated, use {$var} instead in /in/AJqOl on line 44 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 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 Deprecated: Using ${var} in strings is deprecated, use {$var} instead in /in/AJqOl on line 44 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 7.0.0 - 7.0.20, 7.1.0 - 7.1.25, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28
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�>�$%" } }

preferences:
178.35 ms | 404 KiB | 188 Q