Php 函数中未定义从单独脚本加载的变量

Php 函数中未定义从单独脚本加载的变量,php,Php,我使用settings.php存储应用程序的常规设置。加载此设置文件时,我可以在脚本本身中使用settings.php中定义的变量,但不能在脚本中定义的任何函数中使用 例如,在我的类定义myclass.php中: <?php $preIP = dirname(__FILE__); require_once( "preIP/settings.php" ); class MyClass { ... public function foo() { echo $variable

我使用settings.php存储应用程序的常规设置。加载此设置文件时,我可以在脚本本身中使用settings.php中定义的变量,但不能在脚本中定义的任何函数中使用

例如,在我的类定义myclass.php中:

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

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

global $variable_from_settings
以前

echo $variable_from_settings;
放一把怎么样

global $variable_from_settings
以前

echo $variable_from_settings;

如果不想从_设置中放入
global$variable_,可以执行以下操作无处不在

echo $GLOBALS['variable_from_settings'];

但是,最好使用单例来包含您的设置,如

中所建议的,如果您不想从设置中放入
全局$variable\u,可以执行以下操作无处不在

echo $GLOBALS['variable_from_settings'];
不过,最好使用单例来包含您的设置,如中所建议的