静态类
TrackerService.php
<?php
/**
* 时间记录
*/
namespace apifrontendservice;
class TrackerService
{
static $start;
static $end;
static $abc = 111;
public function __construct()
{
self::$abc = 222;
}
/**
* 开始时间
*/
public static function onStart(){
self::$start = getMsectime();
}
/**
* 结束时间
*/
public static function onEnd(){
self::$end = getMsectime();
}
/**
* 运行时间
* @return mixed
*/
public static function runTime(){
return self::$end - self::$start;
}
}
执行
echo TrackerService::$abc;
echo "---";
TrackerService::onStart();
sleep(5);
TrackerService::onEnd();
echo TrackerService::runTime();
echo "--";
echo TrackerService::$start;
echo "--";
echo TrackerService::$end;
echo "---";
echo TrackerService::$abc;
exit;
结果代码
111---5072--1595585293466--1595585298538---111
静态类并没有执行__construct()
静态类再次调用的时候也没有对$start, $end 进行重新初始化。
静态类的加载
程序运行前,静态类提前加载到内存中,所以直接使用静态类不涉及到$start, $end 的值被重新赋值为Null