tags:

views:

102

answers:

6

Is there any way I can persist objects in PHP? I am creating some objects in a script, but I have to create them everytime the script is run (losing their state unless I save it to DB).

I know variables can be persisted with $_SESSION global, but should I save objects in it?

If object persistance is not possible, what's the use of OOP in PHP?

+4  A: 

Serialize the object before you store it in the session:

$s_obj = serialize($myObj);
$_SESSION['myObj'] = $s_obj;

and later, to retrieve and reconstruct it:

$s_obj = $_SESSION['myObj'];
$myObj = unserialize($s_obj);
inkedmn
Thanks! I'm adding some links in case anyone else steps by:http://us2.php.net/manual/en/language.oop.serialization.phphttp://us2.php.net/manual/en/function.serialize.php
Gerardo
+3  A: 

There is no need to serialize objects:

<?php

class A
{
    protected $name;
    public function __construct($name) { $this->name = $name; }
    public function getName() { return $this->name; }
}

session_start();

if (isset($_SESSION['obj'])) {
    die( $_SESSION['obj']->getName() );
} 

$_SESSION['obj'] = new A('name');

?>
jcinacio
+1  A: 

Agree with jcinacio, no need to serialize values before inserting into $_SESSION.. php will manage serialize/unserialize for you on each page request/end.

Another way to persist objects/sessions is to save them on file/database, "emulating" the php behaviour. In this case you'll need to serialize values to convert them into strings, and unserialize them once retrieved from database to convert them back to object.

You may also be interested in the __sleep and __wakeup "Magic Methods" [0] of the object you're going to save. These methods are called when serializing/unserializing the object, to perform action such as connecting/disconnecting from a database, etc.

[0] http://php.net/oop5.magic

redShadow
+1  A: 

Note that if your state is truly shared between the various users, you don't want to use $_SESSION. $_SESSION is only available in the same user session - i.e. if you have 50 users on the site at once, every one of them will have to pay the computation penalty at least once.

In those cases, you might want to use a persistent disk-based on in-memory (memcache) cache.

Alex
+1  A: 

Object persistence is possible, but it is not automatically provided. You either need to write it yourself, or use an object layer that does it for you. So you'll probably need a database.

PHP is not an environment where your program responds to multiple page requests over time: instead, your program is invoked to response to a page request and terminates when it's done.

The purpose of object oriented code in PHP is to make it possible to do a whole raft of programming algorithms and styles, and to make it easier to do an even bigger range of coding solutions. Yes, they are instantiated and destroyed within a single page call, so you have to work within that paradigm. Many codebases pass object IDs around between pages or in sessions; as soon as they need the corresponding object, it is instantiated and loaded from persistent storage using that ID. A good object layer will make this easy.

staticsan
A: 

Try a cache like APC http://www.php.net/apc/

rioja