如何定义一个PHP配置文件定义常量,并在项目中的任何地方使用它 [英] how to define a php config file that define constant and use it any where in project
本文介绍了如何定义一个PHP配置文件定义常量,并在项目中的任何地方使用它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何定义一个配置文件,我可以在整个项目中的任何地方使用它,而无需包括每个地方?
how to define a config file that i can use it any where whole project without include every where?
推荐答案
有两种方式,
-
define() :
使用define方法定义的常量可用于整个项目。
define() : The constants you define using define method is available to the whole project.
define('SITE_URL' , '/path/to/docroot/');
echo SITE_URL;
类常量:整个类都可以使用
class MyConfig{
const SITE_URL = '/path/to/docroot/';
}
echo MyConfig::SITE_URL;
这篇关于如何定义一个PHP配置文件定义常量,并在项目中的任何地方使用它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文