php - Call a session inside a function with object oriented php
问题描述
I'm new to oop and some lines of my code are only approximate, to indicate what I want to achieve.
I want to create a session with php within a function within a class and call it in another function.
class My_beautiful_class
{
public function index()
{
$_SESSION['ciao'] = 'ciao';
var_dump($_SESSION);
}
public function anotherfunction()
{
$this->index();
var_dump($_SESSION);
}
}
I just want to understand a concept: in this way, my code will work but, on the other end, it will execute everything it will find in my function index, in my other function anotherfunction. So, if I call two variable with the same name, I could have a problem.
I guess that in theory, I could handle the problem in another way which is that I could create a variable call sessionone for example and send it some values in with my index function:
class My_beautiful_class
{
public sessionone = [];
public function index()
{
$_SESSION['ciao'] = 'ciao';
$this->sessionone = $_SESSION['ciao'];
}
public function anotherfunction()
{
$this->sessionone;
var_dump($_SESSION);
}
}
, but I was wondering if there any way to for example call only one variable that lives in one function with my first approach.
Something like: (My code is wrong on purpose, it is only to indicate what I want to achieve)
public function index()
{
$_SESSION['ciao'] = 'ciao';
}
public function anotherfunction()
{
$this->index( $_SESSION['ciao'] );
}
}
解决方案
The $_SESSION
variable are what is called a Superglobal in PHP: http://php.net/manual/en/language.variables.superglobals.php This gives them a few unique traits.
First, Superglobals are accessible anywhere in your application regardless of scope. Setting the value of a superglobal key in one function will make the value accessible anywhere else in your application that you want to reference it.
Say for example we want to make a class to manage our session. That class may look something like this.
class SessionManager
{
public function __construct()
{
session_start(); //We must start the session before using it.
}
//This will set a value in the session.
public function setValue($key, $value)
{
$_SESSION[$key] = $value;
}
//This will return a value from the session
public function getValue($key)
{
return $_SESSION[$key];
}
public function printValue($key)
{
print($_SESSION[$key]);
}
}
Once we have a class to manage it a few things happen. We can use this new class to add info to the session and also to retrieve it.
Consider the following code:
$session = new SessionManager();
$session->setValue('Car', 'Honda');
echo $session->getValue('Car'); // This prints the word "Honda" to the screen.
echo $_SESSION['Car']; //This also prints the word "Honda" to the screen.
$session->printValue('Car'); //Again, "Honda" is printed to the screen.
Because of a session being a superglobal, once you set a value on the session it will be assessable anywhere in your application, either in or outside of the class itself.
推荐阅读
- spring - 对于 DataSource 和 JMS 事务同步,除了 JmsTransactionManager 之外,我还需要在 JmsTemplate 中配置 setTransacted 吗?
- mod-wsgi - 在 Django/mod_wsgi 虚拟环境中配置 WSGIPythonHome 的问题
- angular - 根据角度的特定条件查看另一个组件中的特定组件
- visual-studio - 如何将 Visual Studio 项目从 Windows 切换到 Linux 容器?
- javascript - 清除链式相关下拉组合框选择中选定(子)类别的所有子类别
- javascript - d3.js 过渡的问题 - 使用 GeoJson 的多边形变形不正确
- javascript - 如何忽略 Nuxtjs 上的全局 CSS?
- c++ - 运行时错误:引用绑定到“value_type”类型的空指针(stl_vector.h)
- node.js - Twilio Video API - 通知/响铃
- visual-studio - Unity,自定义相机控制的鼠标移动脚本有强烈的漂移