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);

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.