tags:

views:

100

answers:

3

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;
?>
A: 

Most frameworks accomplish this by being the central point of control for the entire website. If your web application isn't centralized, then you won't have a centralized configuration file, and won't have centralized access to the configuration.

When you define something using the define() function, it is defined in the global namespace, so any PHP files included from that point on will have access to it. This isn't the greatest situation, since predicates all code that depends on that constant on being included after the define() statement is run.

Andrew Noyes
Ok so in your opinion if using a constant is not the best idea, how would you set a site into debug mode and be able to see this in any file, class. method?
jasondavis
It really depends on your needs and framework. If you're using Code Igniter for instance, you could setup a debug constant just fine and toggle it as need be or based on url. If you're not using a framework and have no common entry point, you could always extend a base class which reads your config information and has properties you can use, or you can have a list of pre-reqs that have to be included on every page.
thismat
A: 

Define a function in an include file which returns the value of your constant?

vincebowdren
+1  A: 

Use defined to check if the constant was defined, instead of accessing its value:

if ( defined( 'DEBUG' ) )
{
    // debug commands here
}

And to disable the debug mode, simply uncomment the define line, so the constant is undefined again.

poke
This is a scope issue, not how to check it
jasondavis
When you don't have the config file included in a file before, you'll get an error when referring to `DEBUG` simply because it is not defined (because the class was accessed alone or something). But when you check if it was defined, you won't get an undefined error, and as long as you include the config file before the class files, everything will work.
poke
I just updated my question with a full source code example and it does not seem to work the way I need it to, any ideas?
jasondavis
Which result do you expect? I copied over the whole code and tested it myself, so here some possible things you might not want: In `any-file.php` the debug text is not printed out, because `DEBUG` is false. The debug text in the class is printed because you only check for defined (not for the value as well). And you get the fatal error because you are trying to echo an object instance (which is not possible, because that object is not a string).
poke
Sorry about the end echo part, I added that in right when I posted, I removed that part. Ok in my config file I want to be able to set the DEBUG constant to true or false, if it is true, then it should run code inside the class if false then it will just skip of that part
jasondavis
jasondavis
poke
Actually I forgot I am assigning it to a local classs var so your very first answer was correct, sorry for the confusion! I just wanted to make sure I understand the full scope before I write a bunch of code
jasondavis