I have asked similar questions before but here is a full demo example code. In PHP how can I access a Constant value that is set in a config file and then included into every page, Including CLASS FILES?
I realize this is some sort of scope issue so what is the best way to access this constant inside my class file below without passing the constant into the object when it ius created?
Right now, my class file thinks that the CoONSTANT DEBUG var is set to true even when it is false. True or false it still shows and I need to make it only work when the constant is set to true, any ideas?
Example config file that sets the constant value
<?PHP
//config.inc.php
define('DEBUG', false);
echo 'config file loaded <br /><br />';
// load class file here
include 'some_class.php';
?>
Our class file that tries to access out Constant set in the config file above
<?PHP
//some_class.php
class Test{
public function __construct()
{
echo 'some_class has been ran <br /><br />';
if ( defined( 'DEBUG' ) )
{
echo 'DEBUG Constant is vissible inside our class file now!!!!! <br /><br />';
}
}
}
?>
Our test page which puts it all together
<?PHP
//any-file.php
include 'config.inc.php';
if(DEBUG){
echo 'DEBUG Constant is vissible inside our Regular file, this is normal and always happens without any trouble though <br /><br />';
}
$test = new Test;
?>