简体   繁体   中英

are 'long-lived' php objects possible?

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

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

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

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.

您可以做的是使用CLI的CLI版本来编写一个'守护进程'应用程序,该应用程序在请求中保持不变并维护状态等,然后有一个常规的基于Web的脚本,可以通过套接字或其他一些机制与之通信( 这里有一个例子)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM