3v4l.org

run code in 300+ PHP versions simultaneously
<?php class CookieStorage { public function addCookie(Cookie $cookie): void { $encodedCookieValue = $this->encodeCookie($cookie->value); setcookie( $cookie->name, $encodedCookieValue, $cookie->options ); $_COOKIE[$cookie->name] = $encodedCookieValue; } public function getCookie(string $cookieName): ?Cookie { $encodedCookieValue = $_COOKIE[$cookieName] ?? null; if (null === $encodedCookieValue) { return null; } $cookieValue = $this->decodeCookie($encodedCookieValue); return new Cookie($cookieName, $cookieValue); } private function encodeCookie(mixed $value): string { return json_encode($value); } private function decodeCookie(string $value): mixed { return json_decode($value); } } class Cookie { readonly public string $name; readonly public mixed $value; readonly public array $options; public function __construct(string $name, mixed $value, array $options = []) { $this->name = $name; $this->value = $value; if ([] === $options) { $options = [ 'expires' => time() + 60 * 60 * 24 * 30, 'secure' => true, 'httponly' => true, ]; } $this->options = $options; } } $storage = new CookieStorage(); $cookie = new Cookie('test', ['one', 'two']); $storage->addCookie($cookie); var_dump($storage->getCookie('test'));
Output for 8.1.0 - 8.1.24, 8.2.0 - 8.2.11
object(Cookie)#3 (3) { ["name"]=> string(4) "test" ["value"]=> array(2) { [0]=> string(3) "one" [1]=> string(3) "two" } ["options"]=> array(3) { ["expires"]=> int(1700720017) ["secure"]=> bool(true) ["httponly"]=> bool(true) } }
Output for 8.0.1 - 8.0.30
Parse error: syntax error, unexpected identifier "readonly", expecting "function" or "const" in /in/D2RQi on line 37
Process exited with code 255.

preferences:
56.13 ms | 407 KiB | 5 Q