13

In PHP 5 I can declare a const value to a class:

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

But I can also declare public static:

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

In case of a configuration file which I will later use, like:

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

Which of the options do you think is better to be used? (Faster, less memory load, etc...)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
aviv
  • 2,719
  • 7
  • 35
  • 48

6 Answers6

44

The static variable can be changed, the const one cannot. The main consideration should be given to whether the config variables should be able to be altered at run time, not which is faster. The speed difference between the two (if there is any) is so minimal it isn't worth thinking about.

newfurniturey
  • 37,556
  • 9
  • 94
  • 102
Yacoby
  • 54,544
  • 15
  • 116
  • 120
10

use function return global

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

use get instance normal class

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

use static var

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

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

magquast
  • 101
  • 2
  • 2
4

You can use constants for default function argument values, where static variables are not allowed.

extremind
  • 49
  • 3
  • Though a valid point, this doesn't address the question as to "which is better to be used?". Perhaps, if the question was "which is better to use *in this scenario*", yes - but overall, not. – newfurniturey Sep 28 '12 at 17:53
  • 1
    @newfurniturey, I disagree with your comment. Pointing out a limitation of one or the other necessarily shows how the other may be better. And, it seems that since this is precisely the argument of the most popular and the accepted answer, I don't think I'm alone in this. – mkoistinen Dec 20 '12 at 21:26
3

Another difference between const and static is that some variables like arrays are not allowed in class constants, so

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

works, but

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

won't.

HZhang
  • 175
  • 12
1

Also note that because of what Yacoby said above, there are certain things you can do with a static variable you can't do with a CONST, for example assign the result of a runtime method call to the variable.

spronkey
  • 422
  • 3
  • 10
-3

Both 'const' and 'public static' are evil!

As Keith Humm noted - a const can not be the result of a runtime method. And, although I initially thought he was correct in saying you can "assign the result of a runtime method call to (a static) variable", my testing is showing you have to jump through some hoops to do this.

This may seem like a minor point, but it could be the seed of a future disaster. Suppose I am forced to switch to an initialization method for some unforseen reason in the future. I have no problem if this means I have to make a few changes in files under my control. But, what if I change a library that I have sent far and wide to other developers?

I was going to write that, at the very least, I will have to tell everyone else (and 'Future Me') to make changes to THEIR files if they want to upgrade to my new version. In the case of 'const', they will have to add a '$' everywhere they use it. In other scenarios, my testing today indicates they may have to make fewer, but some, changes.

Assuming object oriented code, I follow general advice I have seen for many languages, and needs extra emphasis for PHP - AVOID VISIBLE PROPERTIES of any kind whether const, static or anything else. Use 'getter' methods!

This may seem like a ridiculous performance hit and ugly code style, but unless you can predict the future, it is the only way to go.

See: Tjeerd Visser's answer to How to initialize static variables and give it a bump. Ignore his detractors - if you are that concerned with performance, above possible bugs and other disasters, then switch to another language.

FelixSFD
  • 6,052
  • 10
  • 43
  • 117
DaveWalley
  • 817
  • 10
  • 22