3v4l.org

run code in 300+ PHP versions simultaneously
<?php trait Singleton{ final public static function get_instance(...$args) { static $instance = []; $called_class = get_called_class(); if( !isset( $instance[ $called_class ] ) ) { $instance[$called_class] = new $called_class(...$args); } return $instance[ $called_class ]; } } class Database { use Singleton; protected function __construct(public readonly string $username, public readonly string $password) { } } Database::get_instance('admin', 'admin'); var_dump(Database::get_instance('readonly', 'readonly')); var_dump(Database::get_instance());
Output for 8.1.24 - 8.1.33, 8.2.2 - 8.2.29, 8.3.5 - 8.3.28, 8.4.1 - 8.4.14, 8.4.16, 8.5.0 - 8.5.1
object(Database)#1 (2) { ["username"]=> string(5) "admin" ["password"]=> string(5) "admin" } object(Database)#1 (2) { ["username"]=> string(5) "admin" ["password"]=> string(5) "admin" }
Output for 8.4.15
/bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.35' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.34' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15)
Process exited with code 1.
Output for 8.0.0
Parse error: syntax error, unexpected identifier "string", expecting variable in /in/43NIj on line 21
Process exited with code 255.
Output for 7.4.26
Parse error: syntax error, unexpected 'public' (T_PUBLIC), expecting variable (T_VARIABLE) in /in/43NIj on line 21
Process exited with code 255.

preferences:
88.94 ms | 410 KiB | 5 Q