首页
归档
笔记
树洞
搜索
友言

文章详情

Interesting People Record Interesting.

/ PHP / 文章详情

如何更好地使用 static 关键字

Sonder
2020-04-11
657字
2分钟
浏览 (2.1k)

方法一:

复制代码
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]:[];
}
下一篇 / 使用 phpmailer 群发邮件

🎯 相关文章

💡 推荐文章

🕵️‍♂️ 评论 (0)