# RFC: Object oriented session handlers

- Status: Implemented (PHP 5.4)
- Authors: Arpad Ray arpad@php.net
- Date: 2011-06-20
- Version: 1.1
- Original: https://wiki.php.net/rfc/session-oo
- Page: https://externals.io/rfc/session-oo
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

## Discussions on the mailing list

- [\[RFC\] Object oriented session handlers](https://externals.io/message/52830.md) — started on 2011-06-03, 14 messages
- [\[VOTE\] Object oriented session handlers](https://externals.io/message/53729.md) — started on 2011-07-03, 3 messages
- [\[PHP-CVS\] svn: /php/php-src/ branches/PHP_5_4/NEWSbranches/PHP_5_4/ext/session/config.m4 branches/PHP_5_4/ext/session/config.w32branches/PHP_5_4/ext/session/mod_user.c branches/PHP_5_4/ext/session/mod_user_class.cbranches/PHP_5_4/ex](https://externals.io/message/55429.md) — started on 2011-09-14, 3 messages

---

## 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 an object of this class.

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

- Transparently adding encryption
- Custom GC logic
- Logging

## Example

```php
<?php
class MySession extends SessionHandler {
	public function read($key) {
		return str_rot13(parent::read($key));
	}
 
	public function write($key, $data) {
		return parent::write($key, str_rot13($data));
	}
}
 
session_set_save_handler(new MySession);
 
?>
```

## Usage notes

- Calling session\_set\_save\_handler(object) 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 object.
- Likewise multiple session\_set\_save\_handler(object) 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.

## Patches

<http://spellign.com/patches/php-trunk-session-oo12.patch>

<http://spellign.com/patches/php-5.4-session-oo12.patch>

<http://spellign.com/patches/php-trunk-session-oo12-tests.patch>

## Changelog

- 2011-06-03 Arpad Ray: Initial RFC creation
- 2011-06-20 Arpad Ray: Updated to reflect new patches
- 2011-06-20 Arpad Ray: Updated patches
- 2011-06-27 Arpad Ray: Updated patches
