Page 1 of 1

pass Object through sessions PHP 5

Posted: Thu Nov 23, 2006 1:58 am
by ehsun7b
Is it possible to put Objects in the $_SESSION, $_POST or $_GET in PHP 5? If so, how should I recover them after putting???

Please explain it
:?:

Posted: Thu Nov 23, 2006 2:21 am
by feyd
Are we to assume that you've tried it and failed?

Posted: Thu Nov 23, 2006 6:19 am
by ehsun7b
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Yeah
I put an object in the $_SESSION and when I was trying to recover it in the next page I faced this error:

Code: Select all

Fatal error: main() [<a href='function.main'>function.main</a>]: The script tried to execute a method or access a property of an incomplete object. Please ensure that the class definition "Role" of the object you are trying to operate on was loaded _before_ unserialize() gets called or provide a __autoload() function to load the class definition in C:\Program Files\Apache Group\Apache2\htdocs\epeyk\class\_test2.php on line 10
What should I do? I print_r the $_SESSION in the target page and it contains my object in a strange stucture:

Code: Select all

Array
(
    [role] => __PHP_Incomplete_Class Object
        (
            [__PHP_Incomplete_Class_Name] => Role
            [id:private] => 1
            [title:private] => Administrator
            [description:private] => This role has all the permissions
        )

)

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Thu Nov 23, 2006 7:35 am
by Jenk
you need to include the class definition for both pages that will use the object.

page1:

Code: Select all

<?php
session_start();

include 'MyClass.php';

$_SESSION['object'] = new MyClass();

?>
page 2:

Code: Select all

<?php
session_start();

include 'MyClass.php';

$_SESSION['object']->myMethod();

?>

Posted: Thu Nov 23, 2006 11:01 pm
by ehsun7b
I've done this before. :(

Is there any other specification!!!

What about $_POST and $_GET??? :?:

Posted: Thu Nov 23, 2006 11:05 pm
by volka
The class definition must be present before the object is rebuilt from the session data.

Code: Select all

include 'MyClass.php';
session_start();
$_SESSION['object']->myMethod();