3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Session { const SESSION_STARTED = TRUE; const SESSION_NOT_STARTED = FALSE; // El estado de la sesión private $sessionState = self::SESSION_NOT_STARTED; // La única instancia de la clase private static $instance; private function __construct() {} /** * Devuelve la instancia de "sesión". * La sesión se inicializa automáticamente si no existía. * * @return Objeto **/ public static function getInstance() { if ( !isset(self::$instance)) { self::$instance = new self; } self::$instance->startSession(); return self::$instance; } /** * Inicia la sesión. * * @ Return bool TRUE si la sesión se ha inicializado, de lo contrario False. **/ public function startSession() { if ( $this->sessionState == self::SESSION_NOT_STARTED ) { $this->sessionState = session_start(); } return $this->sessionState; } /** * Carga datos de la sesión. * * @return Void **/ public function __set( $name , $value ) { $_SESSION[$name] = $value; } /** * Obtiene datos de sesión. * **/ public function __get( $name ) { if ( isset($_SESSION[$name])) { return $_SESSION[$name]; } } public function __isset( $name ) { return isset($_SESSION[$name]); } public function __unset( $name ) { unset( $_SESSION[$name] ); } /** * Destruye la sesión actual. * * @ Return bool TRUE si la sesión ha sido eliminada, de lo contrario False. **/ public function destroy() { if ( $this->sessionState == self::SESSION_STARTED ) { $this->sessionState = !session_destroy(); unset( $_SESSION ); return !$this->sessionState; } return FALSE; } } // Obtenemos la instancia de sesion $data = Session::getInstance(); // Carga datos en la sesión $data->nickname = 'Someone'; $data->age = 18; // Visualización de datos printf( '<p>My name is %s and I\'m %d years old.</p>' , $data->nickname , $data->age ); /* Se mostrará: Array ( [nickname] => Someone [age] => 18 ) */ printf( '<pre>%s</pre>' , print_r( $_SESSION , TRUE )); // TRUE var_dump( isset( $data->nickname )); // Destruimos la sesión $data->destroy(); // FALSE var_dump( isset( $data->nickname )); ?>
Output for 5.6.38, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 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, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
<p>My name is Someone and I'm 18 years old.</p><pre>Array ( [nickname] => Someone [age] => 18 ) </pre>bool(true) bool(false)
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 <p>My name is Someone and I'm 18 years old.</p><pre>Array ( [nickname] => Someone [age] => 18 ) </pre>bool(true) bool(false)

preferences:
207.6 ms | 402 KiB | 245 Q