rfc:session-oo

This is an old revision of the document!


Object oriented session handlers

Introduction

This patch allows users to extend session handlers in an object oriented fashion.

Currently users must override all six functions and have no way of calling the original ones.

This patch exposes the original handler (files, memcached, etc.) in a new internal class called SessionHandler, and alters session_set_save_handler() to also accept a class name or object.

Example uses of overriding/wrapping individual methods of a handler:

  • Transparently adding encryption
  • Custom GC logic
  • Logging

Example

<?php
class MySession extends SessionHandler {
	public static function open($path, $name) {
		echo 'Open ', session_id(), "\n";
		return parent::open($path, $name);
	}
	public static function read($key) {
		echo 'Read ', session_id(), "\n";
		return parent::read($key);
	}
}
 
session_set_save_handler('MySession');
 
?>

Usage notes

  • Calling session_set_save_handler(class) after session_set_save_handler(a, b, c, d, e, f) wouldn't transparently extend the first call since they share the same storage. However this can be achieved by calling the former functions manually from the class.
  • Likewise multiple session_set_save_handler(class) calls simply replace each other; just extend the new class from the old one instead of SessionHandler to chain them.
  • An E_WARNING error is raised if the (parent) SessionHandler class is used (read/write/close/destroy) before open is called.
  • Calling session_set_save_handler(object) merely gets the class from the object and proceeds as if session_set_save_handler(class) were called.

Patch

Changelog

2011-06-03 Arpad Ray: Initial RFC creation

rfc/session-oo.1307062262.txt.gz · Last modified: 2017/09/22 13:28 (external edit)