Last active
May 3, 2017 16:52
-
-
Save recca0120/249ba4f41f4a78a81af1f81b056e2364 to your computer and use it in GitHub Desktop.
簡單的 session 類別實現
This file contains hidden or 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
interface SessionInterface { | |
public function start(); | |
public function set($key, $value); | |
public function get($key, $default = null); | |
public function remove($key); | |
public function all(); | |
} | |
class NativeSession implemenet SessionInterface { | |
public function start() { | |
return session_start(); | |
} | |
public function set($key, $value) { | |
$_SESSION[$key] = $value; | |
return $this; | |
} | |
public function get($key, $default = null) { | |
return isset($_SESSION[$key]) ? $_SESSION[$key] : $default; | |
} | |
public function remove($key) { | |
unset($_SESSION[$key]); | |
return $this; | |
} | |
public function all(){ | |
return $_SESSION; | |
} | |
} | |
class CookieSession implemenet SessionInterface { | |
public function start() { | |
} | |
public function set($key, $value) { | |
$_COOKIE[$key] = $value; | |
return $this; | |
} | |
public function get($key, $default = null) { | |
return isset($_COOKIE[$key]) ? $_COOKIE[$key] : $default; | |
} | |
public function remove($key) { | |
unset($_COOKIE[$key]); | |
return $this; | |
} | |
public function all(){ | |
return $_COOKIE; | |
} | |
} | |
class ArraySession implemenet SessionInterface { | |
public $data = []; | |
public function start() { | |
} | |
public function set($key, $value) { | |
$this->data[$key] = $value; | |
return $this; | |
} | |
public function get($key, $default = null) { | |
return isset($this->data[$key]) ? $this->data[$key] : $default; | |
} | |
public function remove($key) { | |
unset($this->data[$key]); | |
return $this; | |
} | |
public function all(){ | |
return $this->data; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment