Skip to content

Instantly share code, notes, and snippets.

@adamlutz
Last active August 29, 2015 14:00
Show Gist options
  • Save adamlutz/11223359 to your computer and use it in GitHub Desktop.
Save adamlutz/11223359 to your computer and use it in GitHub Desktop.
a simple static class example to set context using cookies.
<?php
final class BMTNContext {
const COOKIE_NAME = 'bmtn_context';
private function __construct()
{
return FALSE;
}
public static function set($context)
{
return setcookie(BMTNContext::COOKIE_NAME, $context);
}
public static function get()
{
if (array_key_exists(BMTNContext::COOKIE_NAME, $_COOKIE))
{
return $_COOKIE[BMTNContext::COOKIE_NAME];
}
else
{
return NULL;
}
}
public static function clear()
{
if (array_key_exists(BMTNContext::COOKIE_NAME, $_COOKIE))
{
unset($_COOKIE[BMTNContext::COOKIE_NAME]);
}
return TRUE;
}
}
// example useage
BMTNContext::set('category1');
var_dump(BMTNContext::get()); // NOTE: when context is set, it will not be available till the next page load
// because the $_COOKIE superglobal is loaded at the time of page request
// BMTNContext::clear();
// $no_new = new BMTNContext(); // not-allowed / fails
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment