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!
<?
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();
}
}
?>
$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.
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.