将会话变量与对象一起使用

发布于 2024-10-28 06:02:41 字数 259 浏览 1 评论 0原文

我创建了一个对象并分配了如下值:

$car_object =&新车();

$car_object->offer = 'Sale'; 
$car_object->type = 'Sport Car'; 
$car_object->location = "Buffalo, New york";

如何将 $car_object 存储在会话变量中? 如何从会话变量中获取 $car_object ?

I have created an object and assigned values as follows:

$car_object =& new Car();

$car_object->offer = 'Sale'; 
$car_object->type = 'Sport Car'; 
$car_object->location = "Buffalo, New york";

How can I store the $car_object inside a session variable?
How can I get the $car_object out from the session variable?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

仅一夜美梦 2024-11-04 06:02:41

设置为会话:

$car_object = new Car();

$car_object->offer = 'Sale'; 
$car_object->type = 'Sport Car'; 
$car_object->location = "Buffalo, New york";

$_SESSION['car'] = $car_object;

从会话获取:

$car_object = $_SESSION['car'];
echo $car_object->offer;

Set to session:

$car_object = new Car();

$car_object->offer = 'Sale'; 
$car_object->type = 'Sport Car'; 
$car_object->location = "Buffalo, New york";

$_SESSION['car'] = $car_object;

Get from session:

$car_object = $_SESSION['car'];
echo $car_object->offer;
优雅的叶子 2024-11-04 06:02:41

一个更简单的方法是:

class SessionObject
{
    public function __construct($key)
    {
        if(isset($_SESSION[$key]))
        {
            $_SESSION[$key] = array();
        }
        $this->____data &= $_SESSION[$key];
    }

    public function __get($key)
    {
        return isset($this->___data[$key]) ? $this->___data[$key] : null;
    }

    public function __set($key,$value)
    {
        $this->___data[$key] = $value;
    }
}

然后你可以使用这样的东西;

class CarSession extends SessionObject
{
    public function __construct()
    {
        parent::__construct('car'); //name this object
    }
    /*
        * Custom methods for the car object
    */
}

$Car = new CarSession();

if(!$car->type)
{
     $Car->type = 'transit';
}

这有助于建立一个更易于管理的框架来在会话中存储对象。

例如:

class Currentuser extend SessionObject{}
class LastUpload  extend SessionObject{}
class UserData    extend SessionObject{}
class PagesViewed extend SessionObject{}

A more simpler way would be to do:

class SessionObject
{
    public function __construct($key)
    {
        if(isset($_SESSION[$key]))
        {
            $_SESSION[$key] = array();
        }
        $this->____data &= $_SESSION[$key];
    }

    public function __get($key)
    {
        return isset($this->___data[$key]) ? $this->___data[$key] : null;
    }

    public function __set($key,$value)
    {
        $this->___data[$key] = $value;
    }
}

Then you can use something like this;

class CarSession extends SessionObject
{
    public function __construct()
    {
        parent::__construct('car'); //name this object
    }
    /*
        * Custom methods for the car object
    */
}

$Car = new CarSession();

if(!$car->type)
{
     $Car->type = 'transit';
}

this helps for a more manageable framework for storing objects in the session.

for example:

class Currentuser extend SessionObject{}
class LastUpload  extend SessionObject{}
class UserData    extend SessionObject{}
class PagesViewed extend SessionObject{}
梦情居士 2024-11-04 06:02:41

序列化对象并将其存储到会话中是可行的。以下是关于此问题的完整讨论: PHP:在 $_SESSION 中存储“对象”

Serializing the object and storing it into session works. Here is an entire discussion about this: PHP: Storing 'objects' inside the $_SESSION

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文