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

假装没事ソ 提交于 2019-12-18 02:00:48

问题


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

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


回答1:


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.

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



回答2:


do the following:

define("CONSTANT_NAME","value");

if you want to access this constant from other php files, you have to include it:

include_once("thefilewiththeconstant.php");



回答3:


check this

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

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

$b = $a + $b;
 } 

Sum();
echo $b;
?>



回答4:


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)




回答5:


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.




回答6:


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

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



回答7:


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 $GLOBALS or use 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;


来源:https://stackoverflow.com/questions/10691404/what-is-the-best-way-to-define-a-global-constant-in-php-which-is-available-in-al

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!