Page 2 of 2

Posted: Tue Aug 07, 2007 4:44 pm
by AKA Panama Jack
Boy, I hate admitting when I am wrong. ;) But I am wrong. :D

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. :D Thanks guys for opening my blind little eyes. :D

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>
The retrieval code...

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>

Posted: Wed Aug 08, 2007 1:15 am
by niro121
Hey this is great yepiii we are learning.. Good thread. I'm too testing my script as ev0l and others said. Lets reconstruct object in every page and see how the performance is. I can carry the ID in a session variable so no problem. Next if can lets dig in to this singleton pattern and find a way were we can use the object.. Wow today will be so much coding... lets rock..

Hmmm but i found out some thing wrong directly putting object to session variable. But i will check it in my server as well and update you guys. Hmmm thats why i did the serialization. Thanks guys.

Posted: Wed Aug 08, 2007 1:26 am
by niro121
Hey yep AKA Panama Jack, its working awesome. Auto serialization works. Thanks, seems we dont have to do the serialization when we put in to a session variable. Hmmm Now back to reconstruction.

Posted: Thu Sep 20, 2007 10:21 am
by topgear24
Hi niro,

To answer your original question, add a method in your Log class called setInstance, have it look something like this:

Code: Select all

public function setInstance($o) {
		self::$instance = $o;
	}
Then after your unserialize add:

Code: Select all

$log = unserialize($_SESSION['logger']);
$log->setInstance($log);
I could go into more detail, let me know if you have any questions