Assigning callbacks to class methods

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Assigning callbacks to class methods

Post by anjanesh »

Hi

I normally store session data in a database and therefore defining callback functions dealing with sql queries.

Code: Select all

session_module_name("user");
session_set_save_handler("Session_Open", "Session_Close", "Session_Read", "Session_Write", "Session_Remove", "Session_GC");
session_start();
Now I would like to create a Session class with class methods as Session_Open, Session_Close etc.

But how do I assign callbacks to class methods ?

Thanks
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

This will tell you.
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

Some things are best left OOPless I guess.

Code: Select all

<?php
class Session
 {
        private $UserID;
 
        public function __construct($UserID)
         {
                session_module_name("user");
                session_set_save_handler(array($this, "open"),
                                         array($this, "close"),
                                         array($this, "read"), 
                                         array($this, "write"), 
                                         array($this, "remove"),
                                         array($this, "gc")
                                         );
                session_start();
 
                $this->UserID = $UserID;
         }
 
        public function open($path, $name)
         {
                return TRUE;
         }
 
        public function close()
         {                
                return TRUE;
         }
 
        public function read($id)
         {
                $Row = mysql_query("SELECT `Data` ... ");
                return $Row['Data'];
         }
 
        public function write($id, $data)
         {
                $UserID = $this->UserID; // $UserID seems to be 0 when $_SESSIION['var'] = value; is done
                mysql_query("INSERT $id,NOW(),$UserID,$data ... ON DUPLICATE KEY UPDATE ... $data"); // echo $sql
                return TRUE;
         }
 
        public function remove($id)
         {
                mysql_query("DELETE ... ");
         }
 
        public function gc()
         {
                return TRUE;
         }
 }
?>

Code: Select all

$UserID = 1;
$sess = new Session($UserID);
$_SESSION['test_var'] = 'xxx';
Even though this worked, but this statement $_SESSION['test_var'] = 'xxx'; worked only because of SessionID being primary in the
database and ON DUPLICATE KEY UPDATE `Data` did the job.
 
When I inserted an echo in the session write method, I found that $UserID got a value of 0, when it shouldnt be
mysql_query("INSERT $id,NOW(),$UserID,$data ... ON DUPLICATE KEY UPDATE ... $data"); was

Code: Select all

INSERT INTO `sessions` VALUES ('f1a040midjfer4ga92tb6992f1', NOW(), '0', 'test_var|s:3:\"xxx\";') ON DUPLICATE KEY UPDATE `Data` = 'test_var|s:3:\"xxx\";'
I guess it doesnt make sense creating another instance of Session class except for the constructor call which passes the right $UserID value to the first write() call.
 
And how is this treated ?

Code: Select all

                session_set_save_handler(array($this, "open"),
                                         array($this, "close"),
                                         array($this, "read"), 
                                         array($this, "write"), 
                                         array($this, "remove"),
                                         array($this, "gc")
                                         );
 
Or should I make these functions static ? The only issue with this is the class member data that may be required when reading/writing sessions.
Post Reply