Posted: Tue Aug 07, 2007 4:44 pm
Boy, I hate admitting when I am wrong.
But I am wrong. 
You can have persistence as long as you make sure to include the class code in EVERY PHP script that will be using that object data. When you store an object to a session variable it stores only the object variable data but you still need to provide the class code to be able to use that data in other PHP scripts.
I always thought this was impossible but now that I know about it this opens up all kinds of new possabilities.
Thanks guys for opening my blind little eyes. 
You can test this out yourself.
The retrieval code...
You can have persistence as long as you make sure to include the class code in EVERY PHP script that will be using that object data. When you store an object to a session variable it stores only the object variable data but you still need to provide the class code to be able to use that data in other PHP scripts.
I always thought this was impossible but now that I know about it this opens up all kinds of new possabilities.
You can test this out yourself.
Code: Select all
<?php
session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=ISO-8859-1">
<title>Test</title>
</head>
<body>
Create the object and store data.<br><br>
<?php
class test
{
var $something;
function mytest($data)
{
$this->something = $data;
}
function gettest()
{
return $this->something;
}
}
$myobject = new test();
$myobject->mytest("This worked");
$_SESSION['myobject'] = $myobject;
echo "\$myobject->gettest() = " . $myobject->gettest();
?>
<br>
<a href="testfinish.php">Click Me</a>
</body>
</html>Code: Select all
<?php
session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=ISO-8859-1">
<title>Test</title>
</head>
<body>
New page: retrieve the stored data from the object. <br><br>
<?php
class test
{
var $something;
function mytest($data)
{
$this->something = $data;
}
function gettest()
{
return $this->something;
}
}
$myobject = $_SESSION['myobject'];
echo "\$myobject->gettest() = " . $myobject->gettest();
?>
<br>
<a href="testsession.php">Click Me</a>
</body>
</html>