Skip to content

Instantly share code, notes, and snippets.

@narfbg
Created March 19, 2014 14:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save narfbg/9643416 to your computer and use it in GitHub Desktop.
Save narfbg/9643416 to your computer and use it in GitHub Desktop.
Pseudo-code explaining how OOP interface to sessions should work in PHP 5.6
<?php
/**
* How OOP interface to PHP sessions should look like in version 5.6.
*
* @author Andrey Andreev
*
* Notes:
* - NO other classes and/or interfaces are necessary/useful and therefore shouldn't exist.
* - Probably a good idea to consider making SessionHandler an abstract class.
*
* Ideas for next major version:
* - Split SessionHandler into SessionFilesHandler, SessionMemcacheHandler, etc.
* - Drop support for the pre-5.4-style session_set_save_handler() usage and only accept classes implementing SessionHandlerInterface.
*/
interface SessionHandlerInterface {
public function close();
public function destroy($session_id);
public function gc($maxlifetime);
public function open($save_path, $session_id);
public function read($session_id);
public function write($session_id, $data);
/* Add in next major version:
public function updateTimestamp($session_id);
*/
}
class SessionHandler implements SessionHandlerInterface {
public function __construct(array $options)
{
/* The method itself is dummy, for now. It should assign
* $option elements as properties in next major version.
*
* session_start() should pass session.* ini settings
* and/or any parameters that it accepts to this method.
* This allows userland code to get them without numerous
* ini_get() calls.
*/
}
public function close() { /* works as is */ }
// Remove in next major version
public function create_sid()
{
/**
* E_DEPRECATED if the userland handler overloads it,
* E_ERROR if userland handler called it from createId().
*/
return self::createId();
}
public function createId()
{
/* Do what create_sid() did up to PHP 5.6. */
}
public function destroy($session_id) { /* works as is */ }
public function gc($maxlifetime) { /* works as is */ }
public function open($save_path, $session_id) { /* works as is */ }
public function read($session_id) { /* works as is */ }
public function updateTimestamp($session_id)
{
/**
* Does the userland handler overload it (and called via parent::)?
* - Yes:
* Does session.save_handler support it?
* - Yes:
* Call it.
* - No:
* Call $this->write().
* - No:
* Call $this->write().
*/
}
public function validateId($session_id)
{
/* returns boolean */
}
public function write($session_id, $data) { /* works as is */ }
}
@yohgaki
Copy link

yohgaki commented Mar 19, 2014

I see your intention, but changing existing interface would break apps.
My RFC does not include existing Class/Interface modification which would break existing apps. Since object support of session handler is not forcing certain class/interface now, it will be added as optional methods.

Legacy stuff may be cleaned up in future major releases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment