Are "long-lived" php objects possible?

So, I know that the general idea in PHP is that the whole application loads and runs every time the page loads. But to optimize a meaningful, object-oriented PHP application that needs to be portable ... is it possible to load an object into memory that can be used for each request, and not for each of them ?

I have seen people using the $_SESSION variable for something like this, but it looks like a) ugly, b) will take up a lot of space on the server, and c) doesn't really do what I need, since it is a session by session .

Is there some kind of $_ALL_SESSIONS ?;)

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

+4
source share
5 answers

You are more or less looking for the equivalent ASP / IIS application object in PHP. AFAIK there is no one.

There is an EG (persistent_list), a list of "objects" that were not (necessarily) deleted after the request. It is used by functions such as mysql_pconnect (), pg_pconnect (), ... but it is not available directly through the script code.

memchache has already been mentioned. Can you talk about "purely static objects"?

+3
source

Maybe you can serialize it and save it in memcache? I don't know if it will be faster though.

+3
source

Not by default, no. You will have to use some workaround, whether it be a third-party tool (memcached, DBMS, etc.) or a built-in mechanism (sessions, serialization to a file, etc.). Is this quicker than re-creating an object for each record is up to you.

You can also write a PHP plugin for this. :) Or maybe there is already one. A quick Google search showed nothing, but I did not try very hard.

If you decide to write, you yourself know that this is not as straightforward as it seems. For example, web servers such as Apache generate several child processes for processing requests in parallel. You must be complex in order to receive data to them. Not to mention the correct lock (and lock when the request hangs), processing clusters of web servers, etc.

+1
source

What you can do is use the CLI version of PHP to write the daemon application, which is stored in requests and maintains state, etc., and then has a regular web script interface that can bind it using sockets or another mechanism ( here is one example )

+1
source

If the server is your own machine, then it should be possible to start the process in the background, which will do the "global thing". You can communicate with it using SOAP.

You will need to create a SOAP object.

This is the only way, in my opinion, to create a long-lived object for php. Everything else is just serialization. However, technology outside of PHP may exist for this purpose.

Honestly, I don’t think that your object is large and complex enough to be able to create and fill out longer than it takes to call SOAP. But if creating this object requires a lot of database connections - it is plausible that my idea can help ...

0
source

All Articles