0

I use settings.php to store general settings for my application. When I load this settings file, I can use the variables defined in settings.php in the script itself, but not within any functions I define in it.

For example, in my class definition, myclass.php:

<?php 
$preIP = dirname(__FILE__);
require_once( "preIP/settings.php" );

class MyClass {
  ...
  public function foo() {
    echo $variable_from_settings;
  }
}

The code in the function foo() will not work (the variable will not be defined).

The settings.php file looks like this:

$variable_from_settings = "bar";

Thanks,

Goro
  • 9,919
  • 22
  • 74
  • 108

2 Answers2

2

How about putting

global $variable_from_settings

before

echo $variable_from_settings;
nc3b
  • 15,562
  • 5
  • 51
  • 63
1

You could do the following if you don't want to put global $variable_from_settings; everywhere.

echo $GLOBALS['variable_from_settings'];

However it's probably better to use a singleton to contain your settings as suggested in create superglobal variables in php?

Community
  • 1
  • 1
kb.
  • 1,955
  • 16
  • 22