Page 1 of 1
initialize server side object in memory once
Posted: Tue Aug 18, 2009 5:25 pm
by ke8161
Hello,
I come from Java/C++ background and I am relatively new to PHP. I need to initialize a long list of strings for autocomplete feature. This list is static constant array that will be the same for all users and all sessiosn. I like to initialize it once and keep it in the memory. This is analogous to a static member in Java.
Surprisingly I couldn't find anything silimar in php? I looked at and tried require_once/include_once, but they were called in each session which is not what I wanted. It should be called only once when Apache or php starts up. I looked at global variables but couldn't figure out how to use it to my purpose.
Thanks for the advice!
Kevin
Re: initialize server side object in memory once
Posted: Tue Aug 18, 2009 6:08 pm
by Christopher
Don't me surprised. Just because it is done that way in Java (which uses an application server) does not mean that it should be done that way in PHP. Typically in PHP you load everything you need every request. Just put your array() definition in an include file. That is how it's done in PHP. Essentially what your are doing is premature optimization. I doubt that you will have a performance problem, which would be the only reason not to just include it. If you do then you could do one of the several memory cache systems that work with PHP.
PHP uses a "share nothing" architecture and, unlike Java, tends to solve problems by using standard subsystems like databases, filesystems, etc. Java seems to have this need for everything used to be written in Java.
Re: initialize server side object in memory once
Posted: Tue Aug 18, 2009 7:11 pm
by ke8161
Thank you Christopher. That is what I suspect too.
The problem I encountered is that even I do require_once to initialize the array, the include file is called every time the auto complete handler is called, thus cause a performance hit. I have the site to show this problem:
http://www.questbid.com/?q=postrequest/demo
The category field is an auto-complete. Every typing causes the require_once load again. I guess what is the boundary of require_once?
Re: initialize server side object in memory once
Posted: Tue Aug 18, 2009 7:21 pm
by Eran
You could consider it a performance hit, but as christopher wrote, this is just how PHP works. Including a file is a very fast operation - unless it is very large. You have two alternatives - store the data in a database and query against it or using something like memcache to hold it in resident memory (which would be similar to how you would handle it in Java I guess).
Re: initialize server side object in memory once
Posted: Tue Aug 18, 2009 7:41 pm
by jackpf
Storing it in a database would be more efficient, since rather than loading the whole file that most of, you probably won't need, you can just select the relevant rows from the database.
Re: initialize server side object in memory once
Posted: Tue Aug 18, 2009 7:48 pm
by Christopher
ke8161 wrote:The problem I encountered is that even I do require_once to initialize the array, the include file is called every time the auto complete handler is called, thus cause a performance hit. I have the site to show this problem:
Yes it is a performance hit, but think if it this way. Your server is not using any memory or processor for an application server, so the tiny hit for PHP parsing even a large array is not much. Your demo seems to work fine. Come back when you have a real performance problem and we can discuss solutions.

Re: initialize server side object in memory once
Posted: Wed Aug 19, 2009 11:55 am
by ke8161
Thank you all for the advice! It is very helpful. I want to make sure I did not miss the obvious. Will revisit the issue when the list grows bigger.
Re: initialize server side object in memory once
Posted: Wed Aug 19, 2009 1:31 pm
by Christopher
ke8161 wrote:Thank you all for the advice! It is very helpful. I want to make sure I did not miss the obvious. Will revisit the issue when the list grows bigger.
And I hope you did not think we were "just giving the Java guy grief" because there a number of Java programmers here too. This was a difficult thing for me to understand when I first started using PHP. The two big differences with PHP are this idea of loading every request and also the fact that, unlike in other programming languages, PHP programs are not "long running" applications that need to be much interested in memory management. They only execute for a fraction of a second, load everything, and then everything is freed when done.
If you do end up with performance problems, come back and we can discuss solutions -- both in the code and subsystems like memcached, APC, databases, etc.