How to realize that when a time-consuming function or class method is called multiple times, the operation is performed for the first time, and the result is returned directly when the function is called again?
<?php class MyDate { public static function getCurrentDate() { static $current_date = ''; if (!$current_date) { echo 'only run one time'; usleep(200000); $current_date = date('Y-m-d H:i:s'); } return $current_date; } public static function getCurrentDate2() { usleep(200000); echo 'run everytime'; return date('Y-m-d H:i:s'); } } $start = microtime(true); $i = 5; while ($i--) { MyDate::getCurrentDate(); } echo microtime(true) - $start; //200ms $start = microtime(true); $i = 5; while ($i--) { MyDate::getCurrentDate2(); } echo microtime(true) - $start; //1s