Re: Global or include?

From: Date: Thu, 06 Jan 2011 15:22:59 +0000
Subject: Re: Global or include?
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-310522@lists.php.net to get a copy of this message
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!


Attachment: [application/pkcs7-signature] S/MIME Cryptographic Signature smime.p7s
« previous php.general (#310522) next »