[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 299
  • Last Modified:

Copying user defined object back from Session?

Howdy folks!

I'm in an experimenting mood (this being a holiday and me being out of beer), so I decided to try and create a user-defined object in php. Now, to complicate things a bit, I want the state of the object at the time of rendering (it'll be some sort of html table in the end, based on a query or recordset) to be saved to the session. That works good I think. But what doesn't work is getting the object from the session back to my object. I've added something to unset unnecesary objects. This is the file that does the work:

<?

$UDCONTROL_PREFIX = "UDControl";

session_start();

if (!isset($_SESSION[$UDCONTROL_PREFIX])) {
      $_SESSION[$UDCONTROL_PREFIX] = Array();
}

foreach ($_SESSION[$UDCONTROL_PREFIX] as $Key => $Value) {
      if ($Value->getPageUri() != $_SERVER["SCRIPT_NAME"]) {
            unset($_SESSION[$UDCONTROL_PREFIX][$Key]);
      }
}

class UDControl {
      
      function UDControl($strObjectName, $strPage) {
            global $UDCONTROL_PREFIX;
            
            if (isset($_SESSION[$UDCONTROL_PREFIX][$this->getObjectName()])) {
                  $this = $_SESSION[$UDCONTROL_PREFIX][$this->getObjectName()];
            }
            else {
                  $this->setObjectName($strObjectName);
            }
            
            $this->setPageUri($strPageUri);
      }
      
      function setSource($strSource) { $this->Source = $strSource; }
      function getSource() { return $this->Source; }
      
      function setObjectName($strObjectName) { $this->ObjectName = $strObjectName; }
      function getObjectName() { return $this->ObjectName; }
      
      function setPageUri($strPageUri) { $this->PageUri = $strPageUri; }
      function getPageUri() { return $this->PageUri; }
      
      function Render() {
            global $UDCONTROL_PREFIX;
            
            $_SESSION[$UDCONTROL_PREFIX][$this->getObjectName()] = $this;
            
            return $this->getSource();
      }
      
}

?>

And here's the error:

Fatal error: Cannot re-assign $this in ****\test\udc.php on line 26

So, any decent way to copy the object back from the session?
0
NeoTeq
Asked:
NeoTeq
  • 2
  • 2
1 Solution
 
dr_dedoCommented:
to save object into session or file or db, u need to serialize it first
$_SESSION['x'] = serialize($myObject);

to use it, you have to unserialize it
$myObject = unserialize($_SESSION['x'])
0
 
aminerdCommented:
The offending line is:

$this = $_SESSION[$UDCONTROL_PREFIX][$this->getObjectName()];

You can't assign $this _at all_ (unless you're outside of a class, but that's an entirely different story) because it's really more a keyword than a variable.

Serialization and unserialization of objects in Sessions should be completely automatic. In other words, once you place an object into a session variable, you don't really have to do any work to get it back out. If you're looking for a function that will return the object with the corresponding objectName from the session if it exists, or create a new object if it doesn't, try something like:

      class UDControl
      {
            
            public static function Get_Object($objectName, $pageURI)
            {
                  
                  global $UDCONTROL_PREFIX;
                  
                  if (isset($_SESSION[$UDCONTROL_PREFIX][$objectName]) && ($_SESSION[$UDCONTROL_PREFIX][$objectName] instanceof UDControl))
                  {
                        
                        $object = $_SESSION[$UDCONTROL_PREFIX][$objectName];
                        $object->setPageURI($pageURI);
                        
                  }
                  else
                  {
                        $object = new UDControl($objectName, $pageURI);
                  }
                  
                  return $object;
                  
            }
            
            public function __construct($objectName, $pageURI)
            {
                  
                  $this->ObjectName = $objectNAme;
                  $this->PageUri = $pageURI;
                  
                  return;
                  
            }
            
            [...]
            
      }

Then you can call:

$object = UDControl::GetObject('myname', 'mypage');

(NOTE: This code is written for PHP5... I can port to PHP4 if needed.)
0
 
NeoTeqAuthor Commented:
Well, I'd like it in 4 if it's not too much trouble :)
0
 
aminerdCommented:
     class UDControl
      {
            
            var $ObjectName;
            var $PageUri;
            
            function Get_Object($objectName, $pageURI)
            {
                  
                  global $UDCONTROL_PREFIX;
                  
                  if (isset($_SESSION[$UDCONTROL_PREFIX][$objectName]) && (strtolower(get_class($_SESSION[$UDCONTROL_PREFIX][$objectName])) === 'udcontrol'))
                  {
                              
                              $object = $_SESSION[$UDCONTROL_PREFIX][$objectName];
                              $object->setPageURI($pageURI);
                              
                  }
                  else
                  {
                              $object = new UDControl($objectName, $pageURI);
                  }
                  
                  return $object;
                  
            }
            
            function UDControl($objectName, $pageURI)
            {
                  
                  $this->ObjectName = $objectName;
                  $this->PageUri = $pageURI;
                  
                  return;
                  
            }
            
      }
      
      var_dump(UDControl::Get_Object('name', 'URI'));
0
 
NeoTeqAuthor Commented:
I like it. Elegant, simple. Works like a charm, thanks!
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now