简体   繁体   中英

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?

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

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

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.

更好的方法是检查常量是否分配给任何东西,如果它没有分配给任何东西然后分配它,否则将它分配给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. 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.

// 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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM