如何更好地使用 static 关键字
Sonder
2020-04-11
657字
2分钟
浏览 (2.2k)
方法一:
private static $_user_name_arr = [];
private function getUserNameByUid($uid)
{
if(!isset(self::$_user_name_arr[$uid])){
$user = DB::name("user")->find();
self::$_user_name_arr[$uid] = $user?$user['name']:'';
}
return self::$_user_name_arr[$uid];
}
方法二:
static $locationtimes = array();
public static function getLocationTimes($uid, $dayTime)
{
if(!self::$locationtimes){
$data = DB::name("user")->select();
foreach ($data as $v) {
self::$locationtimes[$v['uid']][]=$v;
}
}
return isset(self::$locationtimes[$uid])?self::$locationtimes[$uid]:[];
}