-
-
Notifications
You must be signed in to change notification settings - Fork 2
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:
- 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.
- 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.
- 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 theTypeSafeGetter
interface that is shared throughout multiple other PHP.Gt repositories. - 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 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")
.
In the next section we will learn about how type safety is enforced.
Php.Gt/Session is a separately maintained component of PHP.Gt/WebEngine