Skip to content

Type safety

Greg Bowler edited this page Jul 1, 2023 · 5 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.


In the next section, learn about how to organise session data using namespaces.

Clone this wiki locally