*SOLVED*Sending Classes as $_SESSION variables

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Deemo
Forum Contributor
Posts: 418
Joined: Sun Jan 18, 2004 11:48 am
Location: Washington DC

*SOLVED*Sending Classes as $_SESSION variables

Post by Deemo »

im working on something right now, and it requires me to send a class in between multiple pages. i would like to do this through Sessions. what i do in my index.php is:

Code: Select all

<?php
$User = new User;      //Declare new class
$User ->Import($INFO, $ID)    //Imports variables from database to $User
$_SESSION['User'] = $User
?>
this works, as if i go to my next page that uses the User class, if i type in

Code: Select all

<?php
echo $_SESSION['User'];
?>
if will return Object, so it IS being passed. but when i do something like

Code: Select all

<?php
echo $_SESSION['User'] ->ID;   //Echo part of class
?>
it returns the following error:
Fatal error: The script tried to execute a method or access a property of an incomplete object. Please ensure that the class definition user of the object you are trying to operate on was loaded _before_ the session was started in /var/www/html/SciFi/shop.php on line 69
what am i doing wrong??
Last edited by Deemo on Tue Mar 09, 2004 1:55 pm, edited 1 time in total.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

You need to include the file that has the class definition before using it via a session.
Eg

Code: Select all

<?php
require_once 'someclass.php';
session_start();
echo $_SESSION['User'] ->ID;
?>
Without the require_once you'll get that error you see.
Deemo
Forum Contributor
Posts: 418
Joined: Sun Jan 18, 2004 11:48 am
Location: Washington DC

Post by Deemo »

thank you thank you thank you thank you :)
Post Reply