Search code examples
phpconnection-poolingpoolingobject-pooling

PHP pooling functionality (not just database connections)


Is it possible to pool data or functionality in PHP?

The amateur-ish PHP code that I write wakes up to handle a response, loads functions, opens database connections, create objects, initialises them, and then - dies after 0.01 secs when the response has been processed, leaving the next request to reload, parse, and run mainly the same thing again.

That's non-sensical, and I find it removes the value of a lot of my work not to have functionality/data/object pooling. For example I can write classes, to find they all get reinitialised with each request - what's the point of me trying to develop a meaningful object structure?

And so: how can I write PHP to pool data and functionality?


Solution

  • There is no 1 solution pooling or persistent state in PHP, it hasn't got an application state like Java, it more or less follows the stateless protocol that is HTTP. What you can do is:

    • Create persistent connections to databases (i.e. they will be reused if you call them with the same parameters, they won't magically exist but you can avoid the overhead of an actual connect).
    • Store objects in sessions to keep a calculated state (they will be serialized, and unserialized on the next request).
    • Route work that requires significant, but one-time, initialization to a daemon running independent from the webserver (a gearman server & workers come to mind).
    • But in the end, if you application needs a global state, maybe PHP just isn't the right solution.