4

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?)

5 Answers 5

3

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" ?

4
  • 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
    Aug 11, 2010 at 8:18
  • @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
    Aug 11, 2010 at 16:04
  • 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.) Aug 15, 2010 at 22:08
  • No, but take a look at the source code of e.g. mysql_pconnect()
    – VolkerK
    Aug 15, 2010 at 22:29
3

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

1
  • 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
    Aug 11, 2010 at 7:58
1

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.

1
  • You can use PHP extensions to have storage that persists across requests, but these are not PHP objects (as in $a = new stdclass).
    – Artefacto
    Aug 11, 2010 at 8:20
1

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)

0

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...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.