Skip to content

Overview

Greg Bowler edited this page May 26, 2023 · 6 revisions

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.

Session implements the following functions:

  • getString(string $name):?string
  • getInt(string $name):?int
  • getFloat(string $name):?float
  • getBool(string $name):?bool
  • getDateTime(string $name):?DateTimeInterface
  • getObject(string $name, class-string<T> $className):?T
  • Along with a function for getting a mixed type, get(string $name):mixed.

These type-safe functions allow your code to be written in a way that knows exactly what data types to expect when being supplied data from the session. This can catch errors early, enhance readability/maintainability, and provide much better IDE support such as autocompletion.

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][namespaces].

Encapsulation using stores

// Link to sub-page


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

Clone this wiki locally