Jump to content

Keeping objects page to page


valaris

Recommended Posts

Hello,

 

So ive made my first OO design, got it working testing it on one page, making sure everything works.  Now the problem is, ive just realized Im not sure how i will keep my objects from page to page(I have many of them).  Right now i have a file with all of my class definitions, that is included by my main object file that declares many objects, and in my index.php page i use these objects.  Now when i goto whatever.php page from index.php , how can i make sure that all my stored data in my objects comes with me?  Is the only way to do this by using some sort of sessions or cookies?

 

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/111427-keeping-objects-page-to-page/
Share on other sites

As long as your class definition exists in a file before the session_start() statement you can use sessions to carry the object variables between pages.

 

The easiest way is to create the object in a session variable -

 

// class definition here ...

session_start();
$_SESSION['object_name'] = new your_object_class();

 

On a different page -

 

// class definition here ...

session_start();

// object in $_SESSION['object_name'] will exist if it existed in the session (the first page created it)

$_SESSION['object_name']->class_method(); // call class method
echo $_SESSION['object_name']->class_variable; // access and echo a class variable

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.