[英]What is the best way to define a global constant in PHP which is available in all files?
What is the best way to define a constant that is global, ie, available in all PHP files? 定义全局常量的最佳方法是什么,即在所有PHP文件中都可用?
UPDATE: What are the differences between constants and class constants? 更新:常量和类常量之间有什么区别? Can they be used interchangeably? 它们可以互换使用吗?
Define your constant in your top .php file, that will be included in all the other scripts. 在顶级 .php文件中定义常量,该常量将包含在所有其他脚本中。 It may be your front controller, your config file, or a file created for this single purpose. 它可能是您的前端控制器,配置文件或为此单一目的而创建的文件。
!defined('MY_CONST') && define('MY_CONST', 'hello');
do the following: 请执行下列操作:
define("CONSTANT_NAME","value");
if you want to access this constant from other php files, you have to include it: 如果你想从其他php文件访问这个常量,你必须包含它:
include_once("thefilewiththeconstant.php");
check this 检查一下
<?php
//Global scope variables
$a = 1;
$b = 2;
function Sum()
{
global $a, $b;
$b = $a + $b;
}
Sum();
echo $b;
?>
When you want to use global constants across PHP files, I feel a good practice is to use Object Oriented principles and define these in relevant classes. 当你想在PHP文件中使用全局常量时,我觉得一个好的做法是使用面向对象的原则并在相关的类中定义它们。 Using the spl_autoload_register() function built into PHP, you can define all your later classes to extend the related "globals class" without worrying about including files manually everywhere. 使用PHP内置的spl_autoload_register()函数,您可以定义所有后续类以扩展相关的“全局类”,而无需担心在任何地方手动包含文件。 All the later child classes of that globals class will always have access to the related global variables. 该全局类的所有后续子类始终可以访问相关的全局变量。 (I say related because it's good design to have global constants in a class be related in some way, rather than everything grouped together with no rhyme or reason) (我说相关是因为将类中的全局常量以某种方式相关联而不是所有组合在一起而没有押韵或理由的设计是好的设计)
Whichever file you create your constant(s) in, you will have to require/include it in the files you will be using them. 无论您在哪个文件中创建常量,都必须在要使用它们的文件中要求/包含它。
Otherwise you can use the $_SESSION global but that requires session initialization by session_start()
at the beginning of your code in each of those files. 否则,您可以使用$ _SESSION全局,但这需要在每个文件的代码开头的session_start()
进行会话初始化。
更好的方法是检查常量是否分配给任何东西,如果它没有分配给任何东西然后分配它,否则将它分配给NULL然后将它分配给你的常量
defined("FOO") ? null : define("FOO", "BAR");
It depends. 这取决于。 In your situation I would go for define()
because it has a more compact syntax in usage. 在你的情况下,我会选择define()
因为它在使用中具有更紧凑的语法。 But define()
can only hold scalar values in PHP < 7.0 In case you need ie an associative array you have no other choice then to go for $GLOBALS
or use PHP >=7.0. 但是define()
只能在PHP <7.0中保存标量值。如果你需要一个关联数组,你没有其他选择,那么去$GLOBALS
或使用PHP> = 7.0。
// Storing a single value works fine with define
define( 'ROOT_DIR', dirname(dirname(__FILE__)) . '/' );
// But not for complex data types like this array
$USERPIC_PARAMS = array(
"user_root" => "images/users",
"padding_length" => 8,
"split_length" => 4,
"hash_length" => 12,
"hide_leftover" => false
);
// Then you need $GLOBALS
$GLOBALS['USERPIC_PARAMS'] = $USERPIC_PARAMS;
// Or in PHP >=7.0
define( 'USERPIC_PARAMS', $USERPIC_PARAMS );
// output your define
echo ROOT_DIR;
// output your $GLOBALS var
echo $GLOBALS['USERPIC_PARAMS'];
// output in PHP >=7.0 your constant
echo USERPIC_PARAMS;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.