Last active
August 29, 2015 14:00
-
-
Save adamlutz/11223359 to your computer and use it in GitHub Desktop.
a simple static class example to set context using cookies.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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