Page 1 of 1

$this = $_SESSION["savedObjectInstance"]; Not working

Posted: Sun Oct 12, 2008 4:42 am
by foobaa
Hi,

I'm trying to have an object save and open itself automatically using the session.

In short, PHP won't let me do:

Code: Select all

$this = $_SESSION["savedObjectInstance"];
It says:
Fatal error: Cannot re-assign $this in ...
Here's a simplified class. The trouble lies in the open() method.

Can anyone recommend a neat way of getting an object to save/load itself to/from the session?

Thanks

Code: Select all

 
<?
 
class MyClass{
    function MyClass(){
        $this->open();
    }
    function process(){
        // do something, then
        $this->save();
    }
    function startSession(){
        if (!session_id()){
            session_start();
        }
    }
    function closeSession(){
        if (session_id()){
            session_write_close();
        }
    }
    function save(){
        $this->startSession();
        $_SESSION["_saved_object_".$this->name] = $this;
        $this->closeSession();
    }
    function open(){
        $this->startSession();
        if (isset($_SESSION["_saved_object_".$this->name])){
            $this = $_SESSION["_saved_object_".$this->name];
        }
        $this->closeSession();
    }
}
 
?>

Re: $this = $_SESSION["savedObjectInstance"]; Not working

Posted: Mon Oct 20, 2008 12:57 am
by novice4eva
$this represents the current instance of the class so that cannot be redeclared. Since you have the object of the class in your hands, i was wondering why would you need to declare it in the class?? It would be helpful if you tell us where you are going with all this. You have your object, use it to call the functions directly.

Re: $this = $_SESSION["savedObjectInstance"]; Not working

Posted: Mon Oct 20, 2008 1:07 am
by foobaa
Sorry - i should have posted that I cleared up the issue.

I realised that a much better way of doing what I wanted was to use a static method to restore the object from the session rather than attempting it from the constructor.

Thanks for your reply tho!

Re: $this = $_SESSION["savedObjectInstance"]; Not working

Posted: Mon Oct 20, 2008 1:12 am
by novice4eva
OR do something like this which i think is a bit :crazy: but i gave it a shot heheh

Code: Select all

 
<?php
    ini_set('display_errors', 1);
    ini_set('error_reporting', E_ALL);
class MyClass
{
    public $myObj;
    public $someValue;
    public $name='zany';
    
    function MyClass()
    {
        $this->open();
    }
    
    function setValue($val)
    {
        $this->myObj->someValue = $val;
    }
    
    function getValue()
    {
        return $this->myObj->someValue;
    }
 
    function process(){
        // do something, then
        $this->myObj->save();
    }
    function startSession(){
        if (!session_id()){
            session_start();
        }
    }
    function closeSession(){
        if (session_id()){
            session_write_close();
        }
    }
    function save(){
        $this->myObj->startSession();
        $_SESSION["_saved_object_".$this->myObj->name] = $this->myObj;
        $this->myObj->closeSession();
    }
    function open(){
        $this->startSession();
        if (isset($_SESSION["_saved_object_".$this->name]))
        {   
            $this->myObj = $_SESSION["_saved_object_".$this->name];
        }
        else
        {
            $this->myObj = $this;
        }
        $this->myObj->closeSession();
    }
    
    function doSomething()
    {
        $this->myObj->show();
    }
}
 
$obj = new MyClass();
$obj->setValue(8);
$obj->save();
echo $obj->getValue();
 
$obj1 = new MyClass();
echo $obj1->getValue();
?>
 
Anyways, i am not comfortable with doing anything like this though!! :mrgreen:

Re: $this = $_SESSION["savedObjectInstance"]; Not working

Posted: Mon Oct 20, 2008 1:26 am
by foobaa
I can't say my code was flawless LOL... oh well... it was just a rushed mock up to make the point :)