Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

move object from 1 page to another?

Hay guys. I'm kinda new to OOP in PHP. I've learnt how to write and create objects. Is there a way to take an object and pass it to another script? either using GET or POST or SESSION or whatever. If there isn't how would i assign an object some variables on one page, then assign the same object more variables on another page?

Thanks

like image 532
dotty Avatar asked Oct 13 '09 14:10

dotty


1 Answers

You can store objects in the session but you need to include the file which contains the class definition before calling session_start() (or use class autoloading and set this up before you start the session). For example:

On every page:

//include class definition
require('class.php');

//start session
session_start();

1st page:

$object = new class();
$object->someProperty = 'hello';

//store in session
$_SESSION['object'] = $object;

Subsequent pages:

$object = $_SESSION['object'];

//add something else, which will be stored in the session
$object->anotherPropery = 'Something';
like image 164
Tom Haigh Avatar answered Sep 30 '22 17:09

Tom Haigh