I'm coming from a J2EE development environment, and by there, I used a Servlet context to hold all the website meta data (title, description ...) for the whole application (some kind of a session for the application when running) as my meta data are retrieved from Database. In PHP, the context is different, we don't deploy or explode a jar or a war file, so for this, instead of asking the database in every page loading, what kind of holder can I use ? (To make it simpler, it will be like a file holding specific little data that will be changed RARELY, like the title of site, and the status of the file (filled or not))

  • 1
    Don't write Java in php. Write php. – user40980 Oct 23 '15 at 18:07
  • Exactly .. that what I'm asking about :) – Chaibi Alaa Oct 23 '15 at 18:16
  • 1
    You are still trying to think in Java though. Don't. Sit down and learn the appropriate idioms and patterns with php and how to apply them. Trying to think "I want a servlet in php" is the wrong way to think about the problem. How to think in php is the subject of many manuals, books and blog posts. – user40980 Oct 23 '15 at 18:18
  • You seem to be asking about a configuration file. Make a PHP file and in it assign the variables you need. Then include that file at the top of your application. That's how most open source PHP projects handle it. – GrandmasterB Oct 23 '15 at 19:31
  • 1
    @ChaibiAlaa Unless you have very high traffic, keeping it in the database would be sufficient. Otherwise, you can implement some caching solutions the way some CMS systems do. They generate PHP files with the data, and regenerate them as the data changes. – GrandmasterB Oct 23 '15 at 20:56

In PHP there is two way to hold data. Sessions and Cookies. Depending about your application choose your mechanism. You can't use the theory of JAVA in PHP. These two are different language and different process to use. The way you can use JAVA is not same for PHP and vise versa.
I don't know from which programming language background you are, but if you learn more about Sessions and Cookies then see the details below links.

If you have any problem with these two then put in the comment. I can help you more to understand.


There has been some development with event loops in PHP, mainly ReactPHP, that kind of work like Node.js, by loading an environment, running an application (during the setup you may load data which you know will be the same throughout the entire application lifecycle) and then reacting accordingly on various programmer-defined inputs.

The problem with PHP is, it is meant to die. Do not be scared if you are a PHP developer, this has nothing to do with PHP as a technology, but rather the application lifecycle. PHP does not have the most advanced garbage collector - please be aware that this may change in the future or perhaps has already changed with the PHP7 version, which I haven't worked a lot with just yet - and ends up taking a lot of RAM because of it, if you do not terminate the app after each request (that's how most PHP applications work). That's why it is meant to die, to dump all the variables knowing none of them will be needed, because separate request has its separate variables.

As Avishake mentioned, you may cache some of the data to the superglobal $_SESSION variable, which is usually available per server, meaning multiple requests have access to the same session. You may lazy load data from the database and then access it from the session. However the performance increase in this case will probably be miniscule.

If what you are after is sharing state among requests, then yes, the $_SESSION variable is a great and very simple tool, but if you want to go through the hassle of caching title and description, you should probably profile your entire application instead and you are very likely to find out, the main bottleneck will be somewhere else and not the single SELECT retrieving the title and description data.

Your Answer

