initialize server side object in memory once

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ke8161
Forum Newbie
Posts: 3
Joined: Tue Aug 18, 2009 5:18 pm

initialize server side object in memory once

Post 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
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: initialize server side object in memory once

Post 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.
(#10850)
ke8161
Forum Newbie
Posts: 3
Joined: Tue Aug 18, 2009 5:18 pm

Re: initialize server side object in memory once

Post 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?
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: initialize server side object in memory once

Post 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).
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: initialize server side object in memory once

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: initialize server side object in memory once

Post 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. ;)
(#10850)
ke8161
Forum Newbie
Posts: 3
Joined: Tue Aug 18, 2009 5:18 pm

Re: initialize server side object in memory once

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: initialize server side object in memory once

Post 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.
(#10850)
Post Reply