note 27241 modified in language.types.object by jmcastagnetto
| From: | jmcastagnetto@php.net | Date: | Fri, 29 Nov 2002 02:11:10 +0000 |
| Subject: | note 27241 modified in language.types.object by jmcastagnetto | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-40091@lists.php.net to get a copy of this message | ||
[Editor's note: PHP already does this for you when registering an object in a session, see the
third paragraph from the bottom in the page "Serializing objects - objects in sessions"]
If you want to use an object with php sessions, you can convert it into a string and then save this
string into a session var
Try this:
----- obj.php
class ShoppingCart{
var $result;
function SayHi(){
$result = "Hi there!";
return $result;
}
}
----- save_obj.php -----
session_start();
session_register('ses_obj');
require ('obj.php');
$Cart = new ShoppingCart;
$ses_obj = serialize ($Cart);
--------- get_obj.php ----
session_start();
$Cart = unserialize ($HTTP_SESSION_VAR['ses_obj']);
$aux = $Cart->SayHi();
print $aux; //prints Hi there!
--was--
If you want to use an object with php sessions, you can convert it into a string and then save this
string into a session var
Try this:
----- obj.php
class ShoppingCart{
var $result;
function SayHi(){
$result = "Hi there!";
return $result;
}
}
----- save_obj.php -----
session_start();
session_register('ses_obj');
require ('obj.php');
$Cart = new ShoppingCart;
$ses_obj = serialize ($Cart);
--------- get_obj.php ----
session_start();
$Cart = unserialize ($HTTP_SESSION_VAR['ses_obj']);
$aux = $Cart->SayHi();
print $aux; //prints Hi there!
http://www.php.net/manual/en/language.types.object.php