【发布时间】:2011-01-06 22:25:18
【问题描述】:
我之前也问过类似的问题,但这里有一个完整的演示示例代码。在 PHP 中,如何访问在配置文件中设置然后包含到每个页面中的常量值,包括类文件?
我意识到这是某种范围问题,那么在下面的类文件中访问此常量而不在创建对象时将常量传递给对象的最佳方法是什么?
现在,我的类文件认为 CoONSTANT DEBUG var 设置为 true,即使它是 false。它仍然显示真或假,我需要让它只在常量设置为真时才起作用,有什么想法吗?
设置常量值的示例配置文件
<?PHP
//config.inc.php
define('DEBUG', false);
echo 'config file loaded <br /><br />';
// load class file here
include 'some_class.php';
?>
我们的类文件试图访问上面配置文件中设置的常量
<?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 />';
}
}
}
?>
我们的测试页面将所有内容放在一起
<?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;
?>
【问题讨论】:
-
问题已更新为更好的代码示例