斗士狗 2010-03-15 14:23 采纳率: 0%
浏览 267
已采纳

定义() vs const

Pretty straightforward question: In PHP, when do you use

define('FOO', 1);

and when do you use

const FOO = 1;

What are the main differences between those two?

转载于:https://stackoverflow.com/questions/2447791/define-vs-const

  • 写回答

10条回答 默认 最新

  • 零零乙 2010-07-07 09:59
    关注

    As of PHP 5.3 there are two ways to define constants: Either using the const keyword or using the define() function:

    const FOO = 'BAR';
    define('FOO', 'BAR');
    

    The fundamental difference between those two ways is that const defines constants at compile time, whereas define defines them at run time. This causes most of const's disadvantages. Some disadvantages of const are:

    • const cannot be used to conditionally define constants. To define a global constant, it has to be used in the outermost scope:

      if (...) {
          const FOO = 'BAR';    // invalid
      }
      // but
      if (...) {
          define('FOO', 'BAR'); // valid
      }
      

      Why would you want to do that anyways? One common application is to check whether the constant is already defined:

      if (!defined('FOO')) {
          define('FOO', 'BAR');
      }
      
    • const accepts a static scalar (number, string or other constant like true, false, null, __FILE__), whereas define() takes any expression. Since PHP 5.6 constant expressions are allowed in const as well:

      const BIT_5 = 1 << 5;    // valid since PHP 5.6, invalid previously
      define('BIT_5', 1 << 5); // always valid
      
    • const takes a plain constant name, whereas define() accepts any expression as name. This allows to do things like this:

      for ($i = 0; $i < 32; ++$i) {
          define('BIT_' . $i, 1 << $i);
      }
      
    • consts are always case sensitive, whereas define() allows you to define case insensitive constants by passing true as the third argument:

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

    So, that was the bad side of things. Now let's look at the reason why I personally always use const unless one of the above situations occurs:

    • const simply reads nicer. It's a language construct instead of a function and also is consistent with how you define constants in classes.
    • const, being a language construct, can be statically analysed by automated tooling.
    • const defines a constant in the current namespace, while define() has to be passed the full namespace name:

      namespace A\B\C;
      // To define the constant A\B\C\FOO:
      const FOO = 'BAR';
      define('A\B\C\FOO', 'BAR');
      
    • Since PHP 5.6 const constants can also be arrays, while define() does not support arrays yet. However arrays will be supported for both cases in PHP 7.

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

    Finally, note that const can also be used within a class or interface to define a class constant or interface constant. define cannot be used for this purpose:

    class Foo {
        const BAR = 2; // valid
    }
    // but
    class Baz {
        define('QUX', 2); // invalid
    }
    

    Summary

    Unless you need any type of conditional or expressional definition, use consts instead of define()s - simply for the sake of readability!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(9条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog