How do i keep singleton class object in a session?

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

User avatar
AKA Panama Jack
Forum Regular
Posts: 878
Joined: Mon Nov 14, 2005 4:21 pm

Post 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>
niro121
Forum Newbie
Posts: 9
Joined: Tue Aug 07, 2007 2:34 am

Post 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.
niro121
Forum Newbie
Posts: 9
Joined: Tue Aug 07, 2007 2:34 am

Post 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.
topgear24
Forum Newbie
Posts: 1
Joined: Thu Sep 20, 2007 10:13 am

Post 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
Post Reply