代码之家  ›  专栏  ›  技术社区  ›  aviv

PHP中的const vs static[重复]

  •  13
  • aviv  · 技术社区  · 16 年前

    const 类的值:

    class config
    {
         const mailserver = 'mx.google.com';
    }
    

    但我也可以宣布 public static :

    class config
    {
         public static $mailserver = 'mx.google.com';
    }
    

    如果是我稍后将使用的配置文件,例如:

    imap_connect(config::$mailserver ...
    imap_connect(config::mailserver ...
    

    6 回复  |  直到 7 年前
        1
  •  43
  •   newfurniturey    13 年前

    静态变量可以更改,常量变量不能更改。主要的考虑应该是配置变量是否可以在运行时更改,而不是哪一个更快。两者之间的速度差(如果有的话)非常小,不值得考虑。

        2
  •  10
  •   magquast    15 年前

    0.0096、0.0053、0.0056、0.0054、0.0072、0.0063、0.006、0.0054、0.0054、0.0055、0.005、0.0057、0.0053、0.0049、0.0064、0.0054、0.0053、0.0053、0.0061、0.0059、0.0076、配置1

    使用get instance normal类

    0.0101、0.0089、0.0105、0.0088、0.0107、0.0083、0.0094、0.0081、0.0106、0.0093、0.0098、0.0092、0.009、0.0087、0.0087、0.0093、0.0095、0.0101、0.0086、0.0088、0.0082、2

    使用静态变量

    0.0029,0.003,0.003,0.0029,0.0029,0.0029,0.003,0.0029,0.003,0.0031,0.0032,0.0031,0.0029,0.0029,0.0029,0.0029,0.0031,0.0029,0.0029,0.0029,0.0029,3

    0.0033、0.0031、0.0031、0.0031、0.0031、0.0031、0.0032、0.0031、0.0031、0.0031、0.0034、0.0031、0.0031、0.0033、0.0031、0.0037、0.0031、0.0031、0.0032、0.0031、4

    function getTime() { 
        $timer = explode( ' ', microtime() ); 
        $timer = $timer[1] + $timer[0]; 
        return $timer; 
    }
    
    $config["time"] = "time";
    
    class testconfig2
    {
        public  $time = "time";
        static $instance;
        static function getInstance()
        {
            if(!isset(self::$instance))
                self::$instance = new testconfig2();
            return self::$instance;
        }
    }
    
    class testconfig3
    {
        static $time = "time";
    }
    
    class testconfig4
    {
        const time = "time";
    }
    
    function getConfig1()
    {
        global $config;
        return $config;
    }
    
    $burncount = 2000;
    $bcount = 22;
    
    for($lcnt =1;$lcnt < $bcount;$lcnt++){
    $start = getTime(); 
    for($i=1;$i< $burncount;$i++)
    {
        $gs=getConfig1();
        $t = $gs["time"];
    } 
    $end = getTime(); 
    echo  round($end - $start,4).', ';
    }
    echo  ' config1<br/>';
    
    
    
    for($lcnt =1;$lcnt < $bcount;$lcnt++){
    $start = getTime(); 
    for($i=1;$i< $burncount;$i++)
    {
        $gs=testconfig2::getInstance();
        $t = $gs->time;
    } 
    $end = getTime(); 
    echo  round($end - $start,4).', ';
    }
    echo  ' config2<br/>';
    
    
    
    for($lcnt =1;$lcnt < $bcount;$lcnt++){
    $start = getTime(); 
    for($i=1;$i< $burncount;$i++)
    {
        $gs=testconfig3::$time;
        $t = $gs;
    } 
    $end = getTime(); 
    echo  round($end - $start,4).', ';
    }
    echo  ' config3<br/>';
    
    
    
    for($lcnt =1;$lcnt < $bcount;$lcnt++){
    $start = getTime(); 
    for($i=1;$i< $burncount;$i++)
    {
        $gs=testconfig4::time;
        $t = $gs;
    } 
    $end = getTime(); 
    echo  round($end - $start,4).', ';
    }
    echo  ' config4<br/>';
    ?>
    

        3
  •  4
  •   extremind    13 年前

        4
  •  3
  •   HZhang    14 年前

    const和static之间的另一个区别是类常量中不允许使用数组之类的变量,因此

    class mytest
    {
        public static $c = array(1=> 'hello', 2=>'world');
    }
    

    工作,但是

    class mytest
    {
        const c = array(1=> 'hello', 2=>'world');
    }
    

    不会。

        5
  •  1
  •   spronkey    16 年前

    还要注意的是,由于Yacoby上面所说的,有一些事情你可以用一个静态变量来做,而不能用CONST来做,比如把运行时方法调用的结果赋给这个变量。

        6
  •  -2
  •   FelixSFD Tushar Panjwani    8 年前

    “const”和“public static”都是邪恶的!

    正如keithhumm所指出的,常量不能是运行时方法的结果。而且,虽然我最初认为他说的“可以将运行时方法调用的结果赋给(静态)变量”是正确的,但我的测试显示,要做到这一点,您必须跳过一些限制。

    这似乎是一个小问题,但它可能是未来灾难的种子。假设我在将来由于某种不可预见的原因被迫切换到初始化方法。我没有问题,如果这意味着我必须在我控制的文件中做一些更改。但是,如果我更改了一个已经发送给其他开发人员的库呢?

    我要写的是,至少,我必须告诉其他人(以及“未来的我”)如果他们想升级到我的新版本,就要对他们的文件进行更改。在“const”的情况下,他们必须在使用它的任何地方添加一个“$”。在其他场景中,我今天的测试表明,他们可能需要做更少的更改,但有些更改。

    假设是面向对象的代码,我遵循我在许多语言中看到的一般建议,并且需要特别强调PHP—避免任何类型的可见属性,无论是常量、静态还是其他任何类型。使用“getter”方法!

    这似乎是一种荒谬的性能冲击和丑陋的代码风格,但除非你能预测未来,否则这是唯一的出路。

    参见:Tjeerd Visser对 How to initialize static variables