Jump to content

access an object created in one page in another page


babaz

Recommended Posts

i have created an object in one page. it has a submit button which upon clicking opens a new page. is it possible to access that created object and its data members in that second page?

 

here is the code for creating the object in the first page.

 

$viewcart = new Cart();
$viewcart->GetProductCart($user);

 

 

You need to use a session variable, either directly -

 

// class definition here ...
session_start();
$_SESSION['viewcart'] = new Cart();
$_SESSION['viewcart']->GetProductCart($user);

 

Or copy to/from a session variable -

// class definition here...
session_start(); // need for any session variable reference
$viewcart = new Cart();
$viewcart->GetProductCart($user);
...
$_SESSION['viewcart'] = $viewcart; // copy the object to a session variable in code or use a class destructor

// second page
// class definition here...
session_start();
$viewcart = $_SESSION['viewcart']; // copy back on the second page

 

The class definition must exist before the session_start() statement so that the object can be recreated from the session data.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.