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

Yaf_Session::offsetGet

PHP function Edit on GitHub ✎

(Yaf >=1.0.0)

Get a session value (ArrayAccess)

Description

Yaf_Session::offsetGet(mixed $name): mixed

Retrieves a session value by key. This method is an alias of Yaf_Session::get and is called when array syntax is used to read a session object.

Parameters

name

The session key to retrieve.

Return Values

The session value, or null if the key does not exist.

Examples

Yaf_Session::offsetGet example

php
<?php
class IndexController extends Yaf_Controller_Abstract
{
    public function indexAction()
    {
        $session = Yaf_Session::getInstance();
        $session->start();

        // reading with array syntax calls offsetGet()
        $userId = $session["user_id"];
        if ($userId === null) {
            return $this->redirect("/account/login");
        }

        echo "logged in as user #", $userId;
    }
}
?>

See Also

  • Yaf_Session::get
  • Yaf_Session::offsetSet
  • Yaf_Session::offsetUnset

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