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

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
foobaa
Forum Commoner
Posts: 40
Joined: Tue Feb 13, 2007 10:36 am

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

Post 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();
    }
}
 
?>
User avatar
novice4eva
Forum Contributor
Posts: 327
Joined: Thu Mar 29, 2007 3:48 am
Location: Nepal

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

Post 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.
foobaa
Forum Commoner
Posts: 40
Joined: Tue Feb 13, 2007 10:36 am

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

Post 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!
User avatar
novice4eva
Forum Contributor
Posts: 327
Joined: Thu Mar 29, 2007 3:48 am
Location: Nepal

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

Post 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:
foobaa
Forum Commoner
Posts: 40
Joined: Tue Feb 13, 2007 10:36 am

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

Post by foobaa »

I can't say my code was flawless LOL... oh well... it was just a rushed mock up to make the point :)
Post Reply