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 git.master, git.master_jit, rfc.property-hooks
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) } }

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
37.54 ms | 406 KiB | 5 Q