Re: [PHP] Global or include?
On Jan 6, 2011 4:24 PM, "Sándor Tamás" wrote: > > In that case you should use include_once in every script. But if you are absolutely sure that all scripts will be processed, you can include it only in one of them, because PHP - in short terms - does a file include, so it will look like as the included file is part of the script. > The global keyword does not do what you think, it only propagates variables between functions without passing them as parameters. In general, you ought to avoid using global, because it can make your script less readable, and / or can lead to logical errors. > > Because I am OOP fan, I'd rather create a static class, using public static fields, so you can reach those variable as like ConstClass::MyConstant. In that way, you have the advantage of simply create new constants, and it doesn't matter how many times you include this file, there will be only one instance. If you include the file several times, you still have the problem of redeclaring the class, so you'd have to wrap the definition in a conditional, which sucks, or use the *_once functions and incur the overhead. Much better to be in control of the includes. Apart from that, it's generally better to use require* instead of include*. If the file is needed, don't continue without it, as the consequences will be unknown. > SanTa > > 2011.01.05. 23:40 keltezéssel, Paul Halliday írta: > >> Say you have 10 or so scripts and a single config file. If you have >> main.php, functions1.php, functions2.php, functions3.php.. >> >> Does is hurt to do an include of the config file in each separate >> script, even if you only need a few things from it, or should you >> just specify what you want with a 'global' within each >> script/function? >> >> Thanks! >> >
Re: [PHP] Global or include?
In that case you should use include_once in every script. But if you are absolutely sure that all scripts will be processed, you can include it only in one of them, because PHP - in short terms - does a file include, so it will look like as the included file is part of the script. The global keyword does not do what you think, it only propagates variables between functions without passing them as parameters. In general, you ought to avoid using global, because it can make your script less readable, and / or can lead to logical errors. Because I am OOP fan, I'd rather create a static class, using public static fields, so you can reach those variable as like ConstClass::MyConstant. In that way, you have the advantage of simply create new constants, and it doesn't matter how many times you include this file, there will be only one instance. SanTa 2011.01.05. 23:40 keltezéssel, Paul Halliday írta: Say you have 10 or so scripts and a single config file. If you have main.php, functions1.php, functions2.php, functions3.php.. Does is hurt to do an include of the config file in each separate script, even if you only need a few things from it, or should you just specify what you want with a 'global' within each script/function? Thanks! smime.p7s Description: S/MIME Cryptographic Signature
Re: [PHP] Global or include?
On Wed, Jan 5, 2011 at 3:27 PM, Nathan Nobbe wrote: > if($cachedConfig !== null) > { >// load file and store value(s) in $cachedConfig > } > "No config for you ... one year!" Sorry, couldn't resist. :p To expand on Nathan's excellent strategy, you could go one further and add functions to config.php to return specific values from the config instead of exposing the raw config object. This will have benefits if you change the structure of the config later and only have to change your access code in one place. It also replaces client code such as $config = load_config(); if ($config['facebook']['enabled']) { ... } with the more readable if (isFacebookEnabled()) { ... } by adding an access function in config.php: function isFacebookEnabled() { $config = load_config(); return (bool) $config['facebook']['enabled']; } You would still require_once 'config.php', but now you don't have to expose knowledge of the config's internal structure to every script that needs to access it. David
Re: [PHP] Global or include?
On Wed, Jan 5, 2011 at 4:27 PM, Nathan Nobbe wrote: > On Wed, Jan 5, 2011 at 3:40 PM, Paul Halliday wrote: > >> Say you have 10 or so scripts and a single config file. If you have >> main.php, functions1.php, functions2.php, functions3.php.. >> >> Does is hurt to do an include of the config file in each separate >> script, even if you only need a few things from it, or should you >> just specify what you want with a 'global' within each >> script/function? >> > > touching on what Nicholas said this could be handled in OOP via the > singleton pattern, however there are ways of dealing w/ it in global > function land as well. > > i see 4 immediate options > . load config from each file (performance hit mitigated if config is a php > array and using opcode caching) > . global variable > . session storage > . config fetching function w/ static variable > > the last option is the one i'd like to describe since i think its the > cleanest, even if you have opcode caching enabled. the function would look > something like this > > function load_config() > { > static $cachedConfig = null; > > if($cachedConfig !== null) > { > // load file and store value(s) in $cachedConfig > } > > return $cachedConfig; > } > > now all you have to do is make load_config() available in all your files, > via something like require_once on the file which defines load_config(). > the result is the configuration will only be read once on a given page > load, thereafter its contents will come from memory. > > this is actually very similar to the singleton approach in OOP. > > -nathan > UMM, check that, the conditional should read if($cachedConfig === null) { // load config file :) $cachedConfig = $someValue; // ... } my bad, lol! -nathan
Re: [PHP] Global or include?
On Wed, Jan 5, 2011 at 3:40 PM, Paul Halliday wrote: > Say you have 10 or so scripts and a single config file. If you have > main.php, functions1.php, functions2.php, functions3.php.. > > Does is hurt to do an include of the config file in each separate > script, even if you only need a few things from it, or should you > just specify what you want with a 'global' within each > script/function? > touching on what Nicholas said this could be handled in OOP via the singleton pattern, however there are ways of dealing w/ it in global function land as well. i see 4 immediate options . load config from each file (performance hit mitigated if config is a php array and using opcode caching) . global variable . session storage . config fetching function w/ static variable the last option is the one i'd like to describe since i think its the cleanest, even if you have opcode caching enabled. the function would look something like this function load_config() { static $cachedConfig = null; if($cachedConfig !== null) { // load file and store value(s) in $cachedConfig } return $cachedConfig; } now all you have to do is make load_config() available in all your files, via something like require_once on the file which defines load_config(). the result is the configuration will only be read once on a given page load, thereafter its contents will come from memory. this is actually very similar to the singleton approach in OOP. -nathan
Re: [PHP] Global or include?
On Jan 5, 2011, at 4:40 PM, Paul Halliday wrote: > Say you have 10 or so scripts and a single config file. If you have > main.php, functions1.php, functions2.php, functions3.php.. > > Does is hurt to do an include of the config file in each separate > script, even if you only need a few things from it, or should you > just specify what you want with a 'global' within each > script/function? > > Thanks! > > -- > Paul Halliday > http://www.pintumbler.org I think that OOP would solve this particular problem best. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Global or include?
Say you have 10 or so scripts and a single config file. If you have main.php, functions1.php, functions2.php, functions3.php.. Does is hurt to do an include of the config file in each separate script, even if you only need a few things from it, or should you just specify what you want with a 'global' within each script/function? Thanks! -- Paul Halliday http://www.pintumbler.org -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php