$this = $_SESSION["savedObjectInstance"]; Not working
Posted: Sun Oct 12, 2008 4:42 am
Hi,
I'm trying to have an object save and open itself automatically using the session.
In short, PHP won't let me do:
It says:
Can anyone recommend a neat way of getting an object to save/load itself to/from the session?
Thanks
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"];Here's a simplified class. The trouble lies in the open() method.Fatal error: Cannot re-assign $this in ...
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();
}
}
?>