4

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?

3 Answers 3

4

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.
Sign up to request clarification or add additional context in comments.

1 Comment

"But in the end, if you application needs a global state, maybe PHP just isn't the right solution" yeah... what I was hoping wasn't the case... But very informative, thanks.
3

PHP is hardly ever the bottleneck. Our servers handle hundreds of requests per second at peak moments. And those are not tiny requests too. It seems illogical, but PHP is actually very fast. And you can use APC cache to cache precompiled PHP files to make it even faster. Then, you can use MemCache to store data, so any query results and data like that can be cached easily without relying on the suboptimal query cache of MySQL.

2 Comments

So - I could use APC cache to hold a set of functions that I normally load using include... That's a bit of relief.
You don't need to actively control the APC cache from PHP. APC can hold the files automatically, so when you include them, you get the precompiled version from the cache, instead of the plain text version from disk.
1

I share similar concerns as yours; but strangely PHP is blindingly fast. Apart from database queries, which should be cached by server, there is only connection issue remains. Which can be pooled easily.

My systems even have to parse a several kb XML file for responses. Still bottle neck is always database server.

This temporary state of PHP has a good side though. A problematic request, crashing the system will not have any adverse effect on the next connection. Seems more stable to me.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.