Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Apr 2009
    Location
    San Diego, CA
    Posts
    64
    Thanks
    2
    Thanked 1 Time in 1 Post

    Constants available in include file?

    I am including a file, left_nav.php, into my index.php file. I have a config.inc.php file that sets a bunch of constants for me to use throughout the site which is also included in index.php. It seems that the left_nav.php file doesn't pick up the constants, but seeing as they are included into the index.php I would expect their global-ness to apply them to the left_nav.php as well.

    Now, I'm new to figuring out the scope stuff, but this seems to the the case elsewhere as I have an autoload function to pull in the appropriate classes as needed. One of those classes is a MYSQl class, and the parent constuctor uses the defined constants to initiate the MYSQL connection with no problem. Am I missing something???
    Last edited by Gremlyn1; 03-08-2010 at 10:03 PM.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Are you ensuring that you're configuration file is included prior to you're left_nav.php file?
    If you're index.php file includes you're config file, it will provide it via the scope of the call. Being constants, these are global anyway. So, when you then include the left_nav.php into the index.php after the configuration information, then yes it should be included at that point as well.
    However, if there is ever an instance when a file can standalone, you may as well include anything and everything it needs at link time instead of depending on it being available at runtime. I'm a huge fan of encapsulating everything dependant together whenever possible - in this situation, I'd be including the configuration file in left_nav.php as well. Just ensure that you're using an include_once/require_once call to ensure that the constants are not attempting a redefinition.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #3
    New Coder
    Join Date
    Apr 2009
    Location
    San Diego, CA
    Posts
    64
    Thanks
    2
    Thanked 1 Time in 1 Post
    The config file is brought in by include_once right after the session is started, before anything else happens. The left_nav is brought in half way down the page. I thought I was reading it right that the global scope would make it available to all files included after.

    Should I be using include_once or require_once? I'm not sure the difference, even after reading up on it...

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Require will throw a fatal error if it cannot include the file, while include will silently fail. Require is nice since it will toss a fatal error without needing to add extra scripting when a failure occurs. Include on the other hand will silently fail and let you continue. Once will only include the file once, so things like constants and functions will not try to redeclare themselves (which will result in a fatal error). I'd use a require when it comes to configurations unless you have a fallback for them.

    Add this to you're left_nav.php file to see if you're constants are declared:
    PHP Code:
    $getConstants get_defined_constants(true);
    $userConstants = &$getConstants['user'];
    print_r($userConstants); 
    Do all of you're constants appear there?
    Also, one thing that should be noted: these are different:
    PHP Code:
    const MYCONST 'CONSTANT VALUE';
    // And
    define('MYCONST''CONSTANT VALUE'); 
    The latter is a global constant. The first is a namespace constant, and is not called with a standard constant replacement call. Ensure you're defining them with define('MYCONST', 'CONSTANT VALUE');.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #5
    New Coder
    Join Date
    Apr 2009
    Location
    San Diego, CA
    Posts
    64
    Thanks
    2
    Thanked 1 Time in 1 Post
    I'm using define() to set the constants. Interestingly, when I added your constant check code to the left_nav, they magically started working and continued to do so when I removed it... Very odd, but thanks for fixing it


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •