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...)
Constant is just a constant, i.e. you can't change its value after declaring. Static variable is accessible without making an instance of a class and therefore shared between all the instances of a class.
The short answer: A const is a promise that you will not try to modify the value once set. A static variable means that the object's lifetime is the entire execution of the program and it's value is initialized only once before the program startup.
The const variable is basically used for declaring a constant value that cannot be modified. A static keyword is been used to declare a variable or a method as static. A const keyword is been used to assign a constant or a fixed value to a variable.
So combining static and const, we can say that when a variable is initialized using static const, it will retain its value till the execution of the program and also, it will not accept any change in its value.
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.
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/>';
?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With