Back to Session class

Method get

public mixed
get
(mixed $name, mixed $default = null)
Get data from the session store
Parameters
  • string $name Name of a variable
  • mixed $default Default value of a variable if not set
Returns
  • mixed Value of a variable
Since
  • 1.5
Class: Session
Project: Joomla

Method get - Source code

/**
 * Get data from the session store
 *
 * @param   string  $name     Name of a variable
 * @param   mixed   $default  Default value of a variable if not set
 *
 * @return  mixed  Value of a variable
 *
 * @since   1.5
 */
public function get($name, $default = null)
{
    // Handle B/C by checking if a namespace was passed to the method, will be removed at 5.0
    if (\func_num_args() > 2) {
        $args = \func_get_args();
        if (!empty($args[2])) {
            @trigger_error('Passing a namespace as a parameter to ' . __METHOD__ . '() is deprecated. ' . 'The namespace should be prepended to the name instead.', E_USER_DEPRECATED);
            $name = $args[2] . '.' . $name;
        }
    }
    if (parent::has($name)) {
        // Parent is used because of b/c, can be changed in Joomla 5
        return parent::get($name, $default);
    }
    /*
     * B/C for retrieving sessions that originated in Joomla 3.
     * A namespace before Joomla 4 has a prefix of 2 underscores (__).
     * This is no longer the case in Joomla 4 and will be converted
     * when saving new values in `self::set()`
     */
    if (strpos($name, '.') !== false && parent::has('__' . $name)) {
        return parent::get('__' . $name, $default);
    }
    // More b/c for retrieving sessions that originated in Joomla 3. This will be removed in Joomla 5
    // as no sessions should have this format anymore!
    if (parent::has('__default.' . $name)) {
        return parent::get('__default.' . $name, $default);
    }
    return $default;
}