Documentation

Session
in package

FinalYes

Session class

This class is responsible for managing the session.

Tags
final

Table of Contents

Constants

FLASH_KEY  : string = 'flash_messages'
Constant representing the key of the session array that stores flash messages.

Properties

$config  : array<string|int, mixed>
$flashMarked  : bool
Flag indicating whether the flash messages have been marked for removal.

Methods

__construct()  : mixed
Constructs a new Session object.
__destruct()  : void
Destructs the object and removes all flash messages from the session.
get()  : mixed
Gets a value from the session.
getFlash()  : mixed
Gets a flash message from the session.
newSession()  : void
Generate a new session id.
remove()  : void
Removes a value from the session.
set()  : void
Sets a value in the session.
setFlash()  : void
Sets a flash message in the session.
sessionDestroy()  : void
Destroys the session and removes all session data.
ensureFlashMarked()  : void
Marks the flash messages for removal.
ensureSessionStarted()  : void
Ensures that the session has been started.
removeFlashMessages()  : void
Removes all flash messages from the session.

Constants

FLASH_KEY

Constant representing the key of the session array that stores flash messages.

protected string FLASH_KEY = 'flash_messages'

Properties

$config

private array<string|int, mixed> $config

$flashMarked

Flag indicating whether the flash messages have been marked for removal.

private bool $flashMarked = false

Methods

__construct()

Constructs a new Session object.

public __construct(array<string|int, mixed> $config) : mixed
Parameters
$config : array<string|int, mixed>

An array containing configuration options for the session.

The following options are supported:

  • sessionName: The name of the session.
  • lifetime: The lifetime of the session in seconds.
  • expireOnClose: Whether the session should expire when the browser is closed.
  • path: The path where the session cookie should be available.

__destruct()

Destructs the object and removes all flash messages from the session.

public __destruct() : void

This method is called when the object is destroyed. It will remove all flash messages from the session.

get()

Gets a value from the session.

public get(string $key) : mixed

This method will get a value from the session. The value is set with the set() method. The value will be removed when the session is closed.

Parameters
$key : string

The key of the value.

Return values
mixed

The value of the key or false if the key does not exist.

getFlash()

Gets a flash message from the session.

public getFlash(string $key) : mixed

This method will get a flash message from the session. The flash message is set with the setFlash() method. The flash message will be removed when the session is closed.

Parameters
$key : string

The key of the flash message.

Return values
mixed

The value of the flash message or false if the flash message does not exist.

newSession()

Generate a new session id.

public newSession() : void

This method will generate a new session id and regenerate the session. This is useful when a user logs in or out.

remove()

Removes a value from the session.

public remove(string $key) : void

This method will remove a value from the session. The value is set with the set() method. The value will be removed when the session is closed.

Parameters
$key : string

The key of the value.

set()

Sets a value in the session.

public set(string $key, mixed $value) : void

This method will set a value in the session. The value can be retrieved with the get() method. The value will be removed when the session is closed.

Parameters
$key : string

The key of the value.

$value : mixed

The value to set.

setFlash()

Sets a flash message in the session.

public setFlash(string $key, mixed $message) : void

This method will set a flash message in the session. The flash message can be retrieved with the getFlash() method. The flash message will be removed when the session is closed.

Parameters
$key : string

The key of the flash message.

$message : mixed

The value of the flash message.

sessionDestroy()

Destroys the session and removes all session data.

protected sessionDestroy() : void

This method will destroy the session and remove all session data. It is called when the object is destroyed.

ensureFlashMarked()

Marks the flash messages for removal.

private ensureFlashMarked() : void

This method will mark all the flash messages for removal. The flash messages will be removed when the session is closed.

ensureSessionStarted()

Ensures that the session has been started.

private ensureSessionStarted([bool $force = false ]) : void

If the session has not been started yet, this method will start it. If the session has already been started, this method will do nothing.

Parameters
$force : bool = false

Whether to force the session to be started even if it has already been started.

removeFlashMessages()

Removes all flash messages from the session.

private removeFlashMessages() : void

This method is called when the object is destroyed. It will remove all flash messages from the session.

On this page

Search results