0

I have a config file which is included in every part of the website. It looks more or less like this (I removed alot of unnecesary lines for the example):

//Company info
define("COMPANY_NAME", "Test Company");
define("COMPANY_EMAIL", "[email protected]");
define("META_KEYWORDS", "");
define("META_DESCRIPTION", "");

//**********************************************************************
//Remember to change this when working Local or on the Production Server
define("DB_SERVER", "127.0.0.1"); 
define("DB_USER", "root");
define("DB_PASSWORD", "");
define("DB_SELECTED_DB", "adminpanel");

One of the lines of this config.php file contains this:

$_ITEM_TYPES = array(
    1=>"admin",
    2=>"page",
    3=>"repository"
);

I'm working in a file where this config.php is included, and I can use constants like COMPANY_NAME, COMPANY_EMAIL and so... but when I'm gonna use the $_ITEM_TYPES array, it throws an undefined variable error :/

I've used the function get_included_files() and the file is included indeed! I don't know why I cannot use this array.

PHP File menu.php

include_once("../config.php");

echo COMPANY_NAME;
//Echoes the value correctly

var_dump($_ITEM_TYPES);
//Throws error

Any ideas? Thank you all beforehand.

4
  • 3
    Show the file where your using the array and including the config file! Commented Oct 6, 2014 at 17:13
  • There it is sir, I edited my question Commented Oct 6, 2014 at 17:17
  • 1
    Look for an exit, die, return, unset etc before the array definition. Commented Oct 6, 2014 at 17:18
  • for me it works fine with these lines of code! I would suggest you do fist what AbraCadaver said and otherwise you post the code between the definitions and the array. Commented Oct 6, 2014 at 17:22

1 Answer 1

1

I'm going to shoot in the dark here. I am 99% sure that the cause of your problem is that you are including this file somewhere else. By using include_once it will not include the file again because it has been included previously, however in another scope which is causing the undefined variable error.

If you want that configuration array to be available globally you can use $GLOBALS, but it's bad practice.

Sign up to request clarification or add additional context in comments.

5 Comments

Seems that it was impossible to solve this in the way I wanted. So I had to move the array from there and put it in the class / file I was going to use it. For flexibility I used a json config file for this part.
Yes. Seems like for some reason it leaves that array out of the scope and cannot be used anywhere. Thing that is hard to do with the system I'm developing because it's a tree of classes, and this config file is included in the top of it. The good thing is that I can use these constants anywhere :P
@JuanBonnett Well of course. I don't know what kind of system that is that has no configuration class. What you have to do is create a class Config that is going to hold that variable and probably many more inside of it as static members so you can access them everywhere.
It makes alot of sense
@JuanBonnett well I didn't invent it, it's how people have been doing it for a long time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.