views:

74

answers:

5

So, I know that the general idea in PHP is that the whole application is loaded and executed every time the page loads. But for optimization of a sizable object-oriented PHP app that needs to be portable…is it possible to load an object into memory that can be used for every request, not recreated for each one?

I've seen people using the $_SESSION variable for something like this, but that seems like it is a) ugly, b) will take up a lot of space on the server, and c) doesn't really do what I need it to as it's a session by session sort of thing.

Is there some sort of $_ALL_SESSIONS? ;)

(Or, approaching the question from a different angle, are purely static objects loaded into memory each time you load the page with a standard Apache mod-php install?)

+3  A: 

Maybe you could serialize it and store it in memcache? I don't know if that would be any faster though.

scompt.com
This just persists the storage of an object across requests, but not the object itself (i.e. it's not capable of performing any execution between requests, and the code for the class still has to be loaded with every request, which the OP is interested in avoiding)
Paul Dixon
+1  A: 

Not by default, no. You'll have to use some workaround, whether it be a 3rd party tool (memcached, DBMS, etc.), or a built-in mechanism (sessions, serializing to a file, etc.) Whether it's faster than recreating the object for each record is up to you.

You could also write a PHP plugin for this. :) Or maybe there already is one. A quick google search revealed nothing but I didn't try very hard.

If you do decide on writing one yourself know that it's not as straightforward as it sounds. For example, webservers such as Apache spawn several child processes for handling requests in parallel. You'll have to be tricky to get data across to them. Not to mention proper locking (and lock breaking if a request hangs), handling of webserver clusters, etc.

Vilx-
You can use PHP extensions to have storage that persists across requests, but these are not PHP objects (as in `$a = new stdclass`).
Artefacto
+3  A: 

You are more or less looking for an equivalent of ASP/IIS's Application object in PHP. AFAIK there isn't one.

There is EG(persistent_list), a list of "objects" that are not (necessarily) removed after a request is served. It's used by functions like mysql_pconnect(), pg_pconnect(), ... But it is not directly accessible by script code.

memchache has already been mentioned. Can you please elaborate on "purely static objects" ?

VolkerK
Those are not objects, they are resources. I know you know that, but simply putting quotes around does not make this clear. And memcache is not really what the OP is looking for, it just another serialization/deserialization method (though it's an efficient one).
Artefacto
@Artefacto: Doesn't necessarily have to be a (php) resource. persistent_list seems to be a "normal" HashTable, so it could hold any (php) value type. But yes, the *_pconnect function store/retrieve resources.
VolkerK
could you get a link about EG(persistent_list) documentation (I take it that's only for extensions and builtin functions…but I wouldn't mind writing some C.)
aharon
No, but take a look at the source code of e.g. mysql_pconnect()
VolkerK
+1  A: 

What you can do is use the CLI version of PHP to write a 'daemon' app which persists across requests and maintains state etc, and then have a regular web based script which can communicate it with via sockets or some other mechanism (here's one example)

Paul Dixon
A: 

If the server is Your own machine then it should be possible to run a process in background that would do the "global thing". You could communicate with it using SOAP.

You would only need to create a SOAP object.

That's the only way I see to really create a long-lived object for php. Everything else is just serialization. There might be a technology outside PHP for that purpose though.

Honestly, I don't think Your object is big and complicated enough for it to be created and populated longer than it takes to make a SOAP call. But if creating this object requires lots of DB connections - it's plausible that my idea could help...

naugtur