在 PHP 中定义所有文件中可用的全局常量的最佳方法是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10691404/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 22:50:02  来源:igfitidea点击:

What is the best way to define a global constant in PHP which is available in all files?

phpvariablesglobal-variablesconstants

提问by cldy1020

What is the best way to define a constant that is global, i.e., available in all PHP files?

定义全局常量(即在所有 PHP 文件中可用)的最佳方法是什么?

UPDATE:What are the differences between constants and class constants? Can they be used interchangeably?

更新:常量和类常量有什么区别?它们可以互换使用吗?

回答by Samy Dindane

Define your constant in your top.php file, that will be included in all the other scripts. It may be your front controller, your config file, or a file created for this single purpose.

顶层.php 文件中定义常量,该常量将包含在所有其他脚本中。它可能是您的前端控制器、您的配置文件或为此单一目的而创建的文件。

!defined('MY_CONST') && define('MY_CONST', 'hello');

回答by Sebastian Breit

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");

回答by Sebastian Breit

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. 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. 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)

当你想跨 PHP 文件使用全局常量时,我​​觉得一个好的做法是使用面向对象的原则并在相关类中定义这些。使用 PHP 内置的 spl_autoload_register() 函数,您可以定义所有以后的类来扩展相关的“全局类”,而不必担心到处手动包含文件。该全局类的所有后续子类将始终可以访问相关的全局变量。(我说相关是因为让类中的全局常量以某种方式相关是一个很好的设计,而不是所有的东西都没有押韵或原因地组合在一起)

回答by Hexodus

It depends. In your situation I would go for define()because it has a more compact syntax in usage. But define()can only hold scalar values in PHP < 7.0 In case you need i.e. an associative array you have no other choice then to go for $GLOBALSor use PHP >=7.0.

这取决于。在你的情况下,我会去,define()因为它在使用中具有更紧凑的语法。但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;

回答by inhan

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()在每个文件中的代码开头进行会话初始化。

回答by Asaithambi Perumal

check this

检查这个

<?php
//Global scope variables
$a = 1;
$b = 2;

 function Sum()
 {
global $a, $b;

$b = $a + $b;
 } 

Sum();
echo $b;
?>

回答by Rust

A better way is to check if the constant is assigned to anything and if it's not assigned to anything then assign it, else assign it to NULL and then assign it to your constant

更好的方法是检查常量是否被分配给任何东西,如果它没有被分配给任何东西然后分配它,否则将它分配给 NULL 然后将它分配给你的常量

defined("FOO") ? null : define("FOO", "BAR");