note 130493 added to language.oop5.static
| From: | mmjunior@live.com | Date: | Wed, 24 Sep 2025 22:45:07 +0000 |
| Subject: | note 130493 added to language.oop5.static | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-222357@lists.php.net to get a copy of this message | ||
I always thought that using
static would be slightly faster... until I decided to test
it and created this simple benchmark.
To my surprise, the processing time is always (slightly) higher with static.
<?php
class Test {
public static $valStatic = 0;
public $valNormal = 0;
public function sumStatic() {
return self::$valStatic + 1;
}
public function sumNormal() {
return $this->valNormal + 1;
}
}
$t = new Test();
$start = microtime(true);
for ($i = 0; $i < 1000000; $i++) {
$t->sumNormal();
}
echo 'Normal: ' . (microtime(true) - $start) . PHP_EOL;
$start = microtime(true);
for ($i = 0; $i < 1000000; $i++) {
$t->sumStatic();
}
echo 'Static: ' . (microtime(true) - $start) . PHP_EOL;
// Outputs like:
// Normal: 0.029894113540649
// Static: 0.03360915184021
?>
----
Server IP: 45.112.84.4
Probable Submitter: 45.112.84.18 (proxied: 191.184.3.188)
----
Manual Page -- https://php.net/manual/en/language.oop5.static.php
Edit -- https://main.php.net/note/edit/130493
Del: integrated -- https://main.php.net/note/delete/130493/integrated
Del: useless -- https://main.php.net/note/delete/130493/useless
Del: bad code -- https://main.php.net/note/delete/130493/bad+code
Del: spam -- https://main.php.net/note/delete/130493/spam
Del: non-english -- https://main.php.net/note/delete/130493/non-english
Del: in docs -- https://main.php.net/note/delete/130493/in+docs
Del: other reasons-- https://main.php.net/note/delete/130493
Reject -- https://main.php.net/note/reject/130493
Search -- https://main.php.net/manage/user-notes.php