diff --git a/src/PSession.php b/src/PSession.php index c9aa8ce..1ebdecf 100644 --- a/src/PSession.php +++ b/src/PSession.php @@ -192,7 +192,20 @@ public function __set(string $key, $value): void */ public function get(string $key, $defaultValue = null) { - return $_SESSION[$key] ?? $defaultValue; + $keys = explode('.', $key); + $session = $_SESSION; + foreach ($keys as $key) { + // allow both object and array access + if (is_array($session) && isset($session[$key])) { + $session = $session[$key]; + } elseif (is_object($session) && isset($session->$key)) { + $session = $session->$key; + } else { + return $defaultValue; + } + } + + return $session ?? $defaultValue; } /** @@ -201,7 +214,24 @@ public function get(string $key, $defaultValue = null) */ public function set(string $key, $value = null): void { - $_SESSION[$key] = $value; + // both object and array access + $keys = explode('.', $key); + $session = &$_SESSION; + foreach ($keys as $key) { + if (is_array($session) && !isset($session[$key])) { + $session[$key] = []; + } elseif (is_object($session) && !isset($session->$key)) { + $session->$key = []; + } + + if (is_array($session)) { + $session = &$session[$key]; + } elseif (is_object($session)) { + $session = &$session->$key; + } + } + + $session = $value; } /**