PHP and define the difference in the const

When you define a constant in PHP, and is available const define both ways, and that they in the end what difference does it make?
  
1. const definitions for the class member variable, its value can not be changed once defined. define define global constants, can be accessed from anywhere.
2. define not be defined in the class, but must be defined in a class const and const variable definition must be accessed by class name :: variable name.
3. const can not define constants in conditional statements.
4. const using a common name for the constant (static scalar), define any expression may be used as the name.
5. const always case-sensitive, however DEFINE () may be defined by a case-insensitive constant third parameter.
6. const easy to read, which itself is a language structure, and define a method, much faster than with the const define defined at compile time.

  If the constants defined in the class, not by DEFINE, and by const, the following example:

<?php
//在类外面通常这样定义常量
define("PHP","111cn.net");
class MyClass
{
    //常量的值将始终保持不变。在定义和使用常量的时候不需要使用$符号
    const constant = 'constant value';

    function showConstant() {
        echo  self::constant . "<br>";
    }
}

echo MyClass::constant . "<br>";

$classname = "MyClass";
echo $classname::constant . "<br>"; // PHP 5.3.0之后

$class = new MyClass();
$class->showConstant();
echo $class::constant."<br>"; // PHP 5.3.0之后
//print_r(get_defined_constants());  //可以用get_defined_constants()获取所有定义的常量
?>
   
   

  Is generally defined outside the class define constants, the constants defined in a class const and const must be accessed via the class name :: variable name. However, the above php5.3 outer support class defined by constant const, see below, this is a ok:

<?php
    const a = "abcdef";
    echo a;
?>
   
   

  About constants of basics, do not say here, in addition to the above, define the difference between const and the other (from the network):

1.const defined conditional statement can not be constant, but can define, as follows:

<?php
    if(1){
        const a = 'java';
    }
    echo a;  //必错
?>
   
   

2.const constant use of a common name, define the expression can be adopted as the name

<?php
const  FOO = 'PHP'; 
for ($i = 0; $i < 32; ++$i) { 
    define('PHP_' . $i, 1 << $i); 
} 
?>
   
   

3.const only accept static scalar, and define any expression can be used.

<?php
const PHP = 1 << 5;    // 错误
define('PHP', 1 << 5); // 正确 
?>
   
   

4.const itself is a language structure. And define a function. So use const speed much faster.

Two things in common: both can not be reassigned.

The following is an excerpt from Rotted_Pencil Bowen: the difference between PHP defined constants, define () vs. const

Introduction
Today Stackoverflow saw a very interesting article, after translation so picked over. NikiC article by one of the members of the PHP developers to write, authoritative natural doubt

Text
in PHP5.3, there are two ways to define constants:

1. Use the const keyword
2. Use the define () method

const FOO = ‘BAR’;
define(‘FOO’,’BAR’);

The fundamental difference between these two approaches is that const defines a constant in the code is compiled, and define a constant is defined only when the code runs. This allows several const will have the following disadvantages:

const can not be used in a conditional statement. If you want to define a global variable, const must be in the outermost layer of the entire code:

if (...) {
    const FOO = 'BAR';    // 无效的
}
// but
if (...) {
    define('FOO', 'BAR'); // 有效的
}
   
   
>

You might ask why would I do that? One of the most common example of this is when if you've been a constant defined in detection:

if (!defined('FOO')) {
    define('FOO', 'BAR');
}
   
   

const can only be used to declare variables (such as numbers, strings, or true, false, null, FILE), and define () can accept expressions. But after PHP5.6 const can also accept expressions of the constants:

const BIT_5 = 1 << 5;    // 在PHP5.6之后有效,之前无效
define('BIT_5', 1 << 5); // 一直有效
   
   

const constants named only with straightforward text, and define () allows you to use any expression to name the constants. So that we can do the following:

for ($i = 0; $i < 32; ++$i) {
    define('BIT_' . $i, 1 << $i);
}
   
   
>

Constant const definition is case sensitive, but allows you to define its third argument is set to true to close its case-sensitive:

define('FOO', 'BAR', true);
echo FOO; // BAR
echo foo; // BAR
   
   

These are a few points you need to pay attention. So now I'll explain below why not involve the above case, I personally always used to use const:

const more readable and beautiful.
const current default namespace defined under constant, and you need to define and specify the use of the full path to the entire namespace:
namespace A \ B \ C;
// if you want to define the constants A \ B \ C \ FOO:
const = FOO 'the BAR ';
dEFINE (' A \ B \ C \ FOO ',' the BAR ');
since PHP5.6, the const can also be defined as a constant array. And define currently does not support this feature, but this feature is implemented in PHP7 in:

const FOO = [1, 2, 3]; // in PHP 5.6 valid
define ( 'FOO', [1 , 2, 3]); // in PHP 5.6 invalid, valid PHP 7.0

Because const is performed at compile time, so it define than the speed a little faster.
Especially when a large number of defined using define constants, PHP's speed will become very slow. People have even invented to avoid this problem, such as apc_load_constantshide

Compared to define, const can double the efficiency of defined constants (on your development machine configured with XDebug, and this difference is even greater). But in query time, there is no difference between the two (because both are used in the same query table)

One final note is that, const can use them in class and interface, and define that can not do this:

class Foo {
    const BAR = 2; // 有效
}
class Baz {
    define('QUX', 2); // 无效
}
   
   

Summary
, unless you need to use expressions or constants defined in a conditional statement, otherwise you are just best to use const for simplicity readability of the code!

Original: https://blog.csdn.net/qq_37107603/article/details/78161317

Guess you like

Origin www.cnblogs.com/showcase/p/12021273.html