| 1 | <?php |
||
| 5 | trait Session |
||
| 6 | { |
||
| 7 | /** |
||
| 8 | * Make a session var name for. |
||
| 9 | * |
||
| 10 | * @param null $name |
||
| 11 | * |
||
| 12 | * @return mixed |
||
| 13 | */ |
||
| 14 | 7 | protected function makeSessionVarName($name = null) |
|
| 15 | { |
||
| 16 | 7 | return $this->config('session_var').(is_null($name) || empty($name) ? '' : '.'.$name); |
|
| 17 | } |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Get a session var value. |
||
| 21 | * |
||
| 22 | * @param null $var |
||
| 23 | * |
||
| 24 | * @return mixed |
||
| 25 | */ |
||
| 26 | 7 | public function sessionGet($var = null, $default = null) |
|
| 27 | { |
||
| 28 | 7 | if ($this->stateless) { |
|
|
|
|||
| 29 | return $default; |
||
| 30 | } |
||
| 31 | |||
| 32 | 7 | return $this->getRequest()->session()->get( |
|
| 33 | 7 | $this->makeSessionVarName($var), |
|
| 34 | $default |
||
| 35 | ); |
||
| 36 | } |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Put a var value to the current session. |
||
| 40 | * |
||
| 41 | * @param $var |
||
| 42 | * @param $value |
||
| 43 | * |
||
| 44 | * @return mixed |
||
| 45 | */ |
||
| 46 | 4 | protected function sessionPut($var, $value) |
|
| 47 | { |
||
| 48 | 4 | if ($this->stateless) { |
|
| 49 | return $value; |
||
| 50 | } |
||
| 51 | |||
| 52 | 4 | $this->getRequest()->session()->put( |
|
| 53 | 4 | $this->makeSessionVarName($var), |
|
| 54 | $value |
||
| 55 | ); |
||
| 56 | |||
| 57 | 4 | return $value; |
|
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Forget a session var. |
||
| 62 | * |
||
| 63 | * @param null $var |
||
| 64 | */ |
||
| 65 | 3 | protected function sessionForget($var = null) |
|
| 66 | { |
||
| 67 | 3 | if ($this->stateless) { |
|
| 68 | return; |
||
| 69 | } |
||
| 70 | |||
| 71 | 3 | $this->getRequest()->session()->forget( |
|
| 72 | 3 | $this->makeSessionVarName($var) |
|
| 73 | ); |
||
| 74 | 3 | } |
|
| 75 | |||
| 76 | abstract protected function config($string, $children = []); |
||
| 77 | |||
| 78 | abstract public function getRequest(); |
||
| 79 | } |
||
| 80 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: