php8.5
Home/ Manual/ yaf / yaf_session/ Yaf_Session::getInstance

Yaf_Session::getInstance

PHP function Edit on GitHub ✎

(Yaf >=1.0.0)

Get the session singleton

Description

Yaf_Session::getInstance(): Yaf_Session

Returns the singleton Yaf_Session instance, creating it if necessary. The session is started automatically on first access.

Parameters Parameters

Return Values

The Yaf_Session singleton instance, or null if the session could not be started.

Examples

Yaf_Session::getInstance example

php
<?php
$session = Yaf_Session::getInstance();
$session->start();
$session->set("user_id", 42);

// ... later, elsewhere in the application
$same = Yaf_Session::getInstance();

var_dump($same === $session);
var_dump($same->get("user_id"));
?>

The above example will output something similar to:

output
bool(true)
int(42)

See Also

  • Yaf_Session::start
  • Yaf_Session::get
  • Yaf_Session::set

Source: reference/yaf/yaf_session/getinstance.xml · from the official PHP manual (php/doc-en)