If I have a file config.php with some configurations in some variables and in some page I include this file, each time the variables will be loaded again in the memory of the server?
I think the answer is yes, but I'm not sure, I don't know to much of the PHP lifecycle. I don't know if I made myself clear, so here is an example. Suppose, I have the config.php file as:
<?php
$databaseConfig = array();
/* Sets the configurations inside the array */
?>
Then my index.php page includes the file config.php. Each time the index is loaded the $databaseConfig array will be loaded into memory or just the first time and so when the page is requested again it doesn't need to load it again? I really think that since PHP is stateless it will load again each time.
1 Answer 1
The file is reloaded and re-parsed at each request. (That may or may not mean it's loaded from disk.) Installing something like APC generally ensures you don't hit the disk or take the parsing hit each time.
The code is re-executed entirely with each request, even if the bytecodes are cached. So, any variable, including $databaseConfig
, is rebuilt with each request. If it comes with notable overhead, you can also use something like APC to cache it in memory.
-
The APC cache will either have to copy the array contents from cache to the PHP process requesting it or use serialization, either will have similar or more overhead than building the array normallyEsailija– Esailija2013年07月31日 23:41:03 +00:00Commented Jul 31, 2013 at 23:41
-
@Esailija APC is only recommended for variable-level caching in this answer if the overhead for generating the structure is notable. E.g., a time-consuming query or service call.svidgen– svidgen2013年08月01日 01:18:03 +00:00Commented Aug 1, 2013 at 1:18
-
But, thanks for noting the reason explicitly! Knowing what your "overhead" compares to helps define whether it's "notable." that said, if there's any question about whether to use APC (or similar) to address the issue, a comparative benchmark should be performed.svidgen– svidgen2013年08月01日 01:22:10 +00:00Commented Aug 1, 2013 at 1:22