PHP中的'const'和'static'有何区别?

14

在 PHP 5 中,我可以向类中声明一个 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个回答

45

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


10

使用函数返回全局变量

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,config1

使用普通类的实例

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,config2

使用静态变量

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,config3

使用常量

0.0033、0.0031、0.0031、0.0031、0.0031、0.0031、0.0032、0.0031、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,config4

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


4

在默认函数参数值中,您可以使用常量,而静态变量是不允许的。


尽管这是一个说得通的观点,但它并没有回答“哪个更好使用?”这个问题。也许,如果问题是“在这种情况下使用哪个更好”,那么是的 - 但总的来说不是这样。 - newfurniturey
1
@newfurniturey,我不同意你的评论。指出其中一个的局限性必然显示出另一个可能更好。而且,似乎由于这正是最受欢迎和被接受的答案的论点,我认为我并不孤立。 - mkoistinen

3
const和static之间的另一个区别在于,一些变量(如数组)不允许作为类常量出现。因此,

...

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

虽然能够工作,但是

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

不会。


5
自 PHP5.6 以后,这不再是真实的情况了。 - Raffaello

1
请注意,正如Yacoby上面所说的那样,使用静态变量可以做一些使用CONST无法实现的事情,例如将运行时方法调用的结果分配给变量。

-3

“const”和“public static”都是有害的!

正如Keith Humm所指出的那样-常量不能是运行时方法的结果。虽然我最初认为他在说你可以“将运行时方法调用的结果分配给(静态)变量”,但我的测试表明,你必须跳过一些障碍才能做到这一点。

这可能看起来像一个小问题,但它可能是未来灾难的种子。假设我不得不因某种不可预见的原因转换为初始化方法。如果这意味着我必须在我控制的文件中进行一些更改,我没有任何问题。但是,如果我更改了一个我已经广泛发送给其他开发人员的库呢?

我本来要写的是,至少我将不得不告诉每个人(包括“未来的我”)如果他们想升级到我的新版本,就必须更改他们的文件。在“const”的情况下,他们将不得不在使用它的地方添加“$”。在其他情况下,我今天的测试表明他们可能需要进行较少但仍需进行一些更改。

假设是面向对象的代码,我遵循了许多语言的通用建议,并且对于PHP来说需要额外强调 - 避免使用任何可见属性,无论是常量、静态变量还是其他任何东西。使用“getter”方法!
这可能看起来像是一个荒谬的性能损失和丑陋的代码风格,但除非你能预测未来,否则这是唯一的出路。
参见:Tjeerd Visser对如何初始化静态变量的回答并给它一个推动。忽略他的批评者 - 如果你那么关心性能,超过可能的错误和其他灾难,那就换一种语言吧。

有趣的 :-/ - AndrewMcLagan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接