Skip to content

Overview

Greg Bowler edited this page Jul 1, 2023 · 6 revisions

In PHP by default, session data is managed through the $_SESSION superglobal. This repository provides an object oriented alternative to using $_SESSION, that comes with the following main benefits:

  1. Encapsulation - Since superglobals are accessible from anywhere in the entire application, they can easily be read and modified by any piece of code, including third party code or plugins. This could lead to security risks or maintainability problems when it becomes difficult to audit where the session data is being manipulated.
  2. Testability - Using superglobals in your code makes testing difficult because it forces your code to rely on global state. During testing, you typically want to isolate pieces of code and run them under controlled conditions. With an object-oriented approach, it becomes much more easy to create mocked session data, for example.
  3. Type-safety - Type safety in programming languages such as PHP offers several advantages, which can lead to more robust, maintainable, and reliable code. The Session class implements the TypeSafeGetter interface that is shared throughout multiple other PHP.Gt repositories.
  4. Maintainability - since all session variables are encapsulated, and the use of namespaces promotes organisation of the stored data, distinct areas of your project become less fragile to changes.

Session namespaces

Session keys are addressable by their name, but can be nested using namespacing in order to encapsulate their data between different areas of your application. It's possible to load session data from nested namespaces by using a dot to separate namespaces, like this: $session->getString("somewhere.deep.inside.NAME").

Read more about namespaces.


In the next section we will learn about how type safety is enforced.

Clone this wiki locally