好湿?好紧?好多水好爽自慰,久久久噜久噜久久综合,成人做爰A片免费看黄冈,机机对机机30分钟无遮挡

主頁 > 知識庫 > PHP常用的類封裝小結【4個工具類】

PHP常用的類封裝小結【4個工具類】

熱門標簽:廣東廣州在怎么申請400電話 400電話蘭州申請請 余姚電話機器人 百度地圖怎樣標注圖標 電銷機器人問門薩維品牌my 開發地圖標注類網站 外呼系統能給企業帶來哪些好處 咸寧銷售電銷機器人系統 百度地圖標注偏差

本文實例講述了PHP常用的類封裝。分享給大家供大家參考,具體如下:

這4個類分別是Mysql類、 分頁類、縮略圖類、上傳類。

Mysql類

?php
/**
 * Mysql類
 */
class Mysql{
  private static $link = null;//數據庫連接
  /**
   * 私有的構造方法
   */
  private function __construct(){}
  /**
   * 連接數據庫
   * @return obj 資源對象
   */
  private static function conn(){
    if(self::$link === null){
      $cfg = require './config.php';
      self::$link = new Mysqli($cfg['host'],$cfg['user'],$cfg['pwd'],$cfg['db']);
      self::query("set names ".$cfg['charset']);//設置字符集
    }
    return self::$link;
  }
  /**
   * 執行一條sql語句
   * @param str $sql 查詢語句
   * @return obj   結果集對象
   */
  public static function query($sql){
    return self::conn()->query($sql);
  }
  /**
   * 獲取多行數據
   * @param str $sql 查詢語句
   * @return arr   多行數據
   */
  public static function getAll($sql){
    $data = array();
    $res = self::query($sql);
    while($row = $res->fetch_assoc()){
      $data[] = $row;
    }
    return $data;
  }
  /**
   * 獲取一行數據
   * @param str $row 查詢語句
   * @return arr   單行數據
   */
  public static function getRow($row){
    $res = self::query($sql);
    return $res->fetch_assoc();
  }
  /**
   * 獲取單個結果
   * @param str $sql 查詢語句
   * @return str   單個結果
   */
  public static function getOne($sql){
    $res = self::query($sql);
    $data = $res->fetch_row();
    return $data[0];
  }
  /**
   * 插入/更新數據
   * @param str $table 表名
   * @param arr $data 插入/更新的數據
   * @param str $act  insert/update
   * @param str $where 更新條件
   * @return bool 插入/更新是否成功
   */
  public static function exec($table,$data,$act='insert',$where='0'){
    //插入操作
    if($act == 'insert'){
      $sql = 'insert into '.$table;
      $sql .= ' ('.implode(',',array_keys($data)).')';
      $sql .= " values ('".implode("','",array_values($data))."')";
    }else if($act == 'update'){
      $sql = 'update '.$table.' set ';
      foreach ($data as $k => $v) {
        $sql .= $k.'='."'$v',";
      }
      $sql = rtrim($sql,',');
      $sql .= ' where 1 and '.$where;
    }
    return self::query($sql);
  }
  /**
   * 獲取最近一次插入的主鍵值
   * @return int 主鍵
   */
  public static function getLastId(){
    return self::conn()->insert_id;
  }
  /**
   * 獲取最近一次操作影響的行數
   * @return int 影響的行數
   */
  public static function getAffectedRows(){
    return self::conn()->affected_rows;
  }
  /**
   * 關閉數據庫連接
   * @return bool 是否關閉
   */
  public static function close(){
    return self::conn()->close();
  }
}
?>

分頁類

?php
/**
 * 分頁類
 * @author webbc
 */
class Page{
  private $num;//總的文章數
  private $cnt;//每頁顯示的文章數
  private $curr;//當前的頁碼數
  private $p = 'page';//分頁參數名
  private $pageCnt = 5;//分欄總共顯示的頁數
  private $firstRow;//每頁的第一行數據
  private $pageIndex = array();//分頁信息
  /**
   * 構造函數
   * @param int $num 總的文章數
   * @param int $cnt 每頁顯示的文章數
   */
  public function __construct($num,$cnt=10){
    $this->num = $num;
    $this->cnt = $cnt;
    $this->curr = empty($_GET[$this->p]) ? 1 : intval($_GET[$this->p]);
    $this->curr = $this->curr > 0 ? $this->curr : 1;
    $this->firstRow  = $this->cnt * ($this->curr - 1);
    $this->getPage();
  }
  /**
   * 分頁方法
   */
  private function getPage(){
    $page = ceil($this->num / $this->cnt);//總的頁數
    $left = max(1,$this->curr - floor($this->pageCnt/2));//計算最左邊頁碼
    $right = min($left + $this->pageCnt - 1 ,$page);//計算最右邊頁碼
    $left = max(1,$right - ($this->pageCnt - 1));//當前頁碼往右靠,需要重新計算左邊頁面的值
    for($i=$left;$i=$right;$i++){
      if($i == 1){
        $index = '第1頁';
      }else if($i == $page){
        $index = '最后一頁';
      }else{
        $index = '第'.$i.'頁';
      }
      $_GET['page'] = $i;
      $this->pageIndex[$index] = http_build_query($_GET);
    }
  }
  /**
   * 返回分頁信息數據
   * @return [type] [description]
   */
  public function show(){
    return $this->pageIndex;
  }
}
?>

縮略圖類

?php
/**
 * 縮略圖類
 * @author webbc
 */
class Thumb{
  private $thumbWidth;//縮略圖的寬
  private $thumbHeight;//縮略圖的高
  private $thumbPath;//縮略圖保存的路徑
  private $sourcePath;//原圖的路徑
  private $sourceWidth;//原圖的寬度
  private $sourceHeight;//原圖的高度
  private $sourceType;//原圖的圖片類型
  /**
   * 構造函數
   * @param str $sourcePath 原圖的絕對路徑
   * @param integer $thumbWidth 縮略圖的寬
   * @param integer $thumbHeight 縮略圖的高
   */
  public function __construct($sourcePath,$thumbWidth=200,$thumbHeight=200){
    //獲取原圖的絕對路徑
    $this->sourcePath = $sourcePath;
    //獲取縮略圖的大小
    $this->thumbWidth = $thumbWidth;
    $this->thumbHeight = $thumbHeight;
    $this->thumbPath = $this->getThumbPath();
    //計算大圖的大小
    list($this->sourceWidth,$this->sourceHeight,$this->sourceType) = getimagesize($this->sourcePath);
  }
  /**
   * 確定縮略圖保存的路徑
   * @return [type] [description]
   */
  private function getThumbPath(){
    $ext = $this->getExt();
    $filename = basename($this->sourcePath,'.'.$ext).'_thumb'.'.'.$ext;
    return $thumbPath = __DIR__.'/'.$filename;
  }
  /**
   * 獲取原圖的擴展名
   * @return str 擴展名
   */
  private function getExt(){
    return pathinfo($this->sourcePath,PATHINFO_EXTENSION);
  }
  /**
   * 檢測原圖的擴展名是否合法,并返回相應類型
   * @return bool/str 原圖的類型
   */
  public function getType(){
    $typeArr = array(
      1 => 'gif',
      2 => 'jpeg',
      3 => 'png',
      15 => 'wbmp'
    );
    if(!in_array($this->sourceType, array_keys($typeArr))){
      return false;
    }
    return $typeArr[$this->sourceType];
  }
  /**
   * 按照縮略圖大小,計算大圖的縮放比例
   * @return float 縮放比例
   */
  public function calculateRate(){
    return min($this->thumbWidth / $this->sourceWidth,$this->thumbHeight / $this->sourceHeight);
  }
  /**
   * 計算大圖按照縮放比例后,最終的圖像大小
   * @param float $rate 縮放比例
   * @return arr 縮放后的圖片大小
   */
  public function getImageSizeByRate($rate){
    $width = $this->sourceWidth * $rate;
    $height = $this->sourceHeight * $rate;
    return array('w'=>$width,'h'=>$height);
  }
  /**
   * 保存成文件
   * @return [type] [description]
   */
  public function saveFile($image){
    $method = "image".$this->getType();
    $method($image,$this->thumbPath);
  }
  /**
   * 進行繪畫操作
   * @return [type] [description]
   */
  public function draw(){
    if(!($type = $this->getType())){
      echo "文件類型不支持";
      return ;
    }
    //創建大圖和小圖的畫布
    $method = "imagecreatefrom".$type;
    $bigCanvas = $method($this->sourcePath);
    $smallCanvas = imagecreatetruecolor($this->thumbWidth, $this->thumbHeight);
    //創建白色畫筆,并給小圖畫布填充背景
    $white = imagecolorallocate($smallCanvas, 255, 255, 255);
    imagefill($smallCanvas, 0, 0, $white);
    //計算大圖的縮放比例
    $rate = $this->calculateRate();
    //計算大圖縮放后的大小信息
    $info = $this->getImageSizeByRate($rate);
    //進行縮放
    imagecopyresampled($smallCanvas, $bigCanvas,
      ($this->thumbWidth - $info['w']) / 2 , ($this->thumbHeight - $info['h']) / 2,
      0, 0, $info['w'], $info['h'], $this->sourceWidth, $this->sourceHeight);
    //保存成文件
    $this->saveFile($smallCanvas);
    //銷毀畫布
    imagedestroy($bigCanvas);
    imagedestroy($smallCanvas);
  }
}
?>

上傳類

meta charset="utf8"/>
?php
/**
 * 文件上傳類
 * @author webbc
 */
class Upload{
  private $allowExt = array('gif','jpg','jpeg','bmp','png','swf');//限制文件上傳的后綴名
  private $maxSize = 1;//限制最大文件上傳1M
  /**
   * 獲取文件的信息
   * @param str $flag 上傳文件的標識
   * @return arr    上傳文件的信息數組
   */
  public function getInfo($flag){
    return $_FILES[$flag];
  }
  /**
   * 獲取文件的擴展名
   * @param str $filename 文件名
   * @return str 文件擴展名
   */
  public function getExt($filename){
    return pathinfo($filename,PATHINFO_EXTENSION);
  }
  /**
   * 檢測文件擴展名是否合法
   * @param str $filename 文件名
   * @return bool 文件擴展名是否合法
   */
  private function checkExt($filename){
    $ext = $this->getExt($filename);
    return in_array($ext,$this->allowExt);
  }
  /**
   * 檢測文件大小是否超過限制
   * @param int size 文件大小
   * @return bool 文件大小是否超過限制
   */
  public function checkSize($size){
    return $size  $this->maxSize * 1024 * 1024;
  }
  /**
   * 隨機的文件名
   * @param int $len 隨機文件名的長度
   * @return str 隨機字符串
   */
  public function randName($len=6){
    return substr(str_shuffle('abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ234565789'),0,$len);
  }
  /**
   * 創建文件上傳到的路徑
   * @return str 文件上傳的路徑
   */
  public function createDir(){
    $dir = './upload/'.date('Y/m/d',time());
    if(is_dir($dir) || mkdir($dir,0777,true)){
      return $dir;
    }
  }
  /**
   * 文件上傳
   * @param str $flag 文件上傳標識
   * @return arr 文件上傳信息
   */
  public function uploadFile($flag){
    if($_FILES[$flag]['name'] === '' || $_FILES[$flag]['error'] !== 0){
      echo "沒有上傳文件";
      return;
    }
    $info = $this->getInfo($flag);
    if(!$this->checkExt($info['name'])){
      echo "不支持的文件類型";
      return;
    }
    if(!$this->checkSize($info['size'])){
      echo "文件大小超過限制";
      return;
    }
    $filename = $this->randName().'.'.$this->getExt($info['name']);
    $dir = $this->createDir();
    if(!move_uploaded_file($info['tmp_name'], $dir.'/'.$filename)){
      echo "文件上傳失敗";
    }else{
      return array('filename'=>$filename,'dir'=>$dir);
    }
  }
}
?>

更多關于PHP相關內容感興趣的讀者可查看本站專題:《php+mysql數據庫操作入門教程》、《php+mysqli數據庫程序設計技巧總結》、《php面向對象程序設計入門教程》、《PHP數組(Array)操作技巧大全》、《php字符串(string)用法總結》、《PHP網絡編程技巧總結》及《php常見數據庫操作技巧匯總》

希望本文所述對大家PHP程序設計有所幫助。

您可能感興趣的文章:
  • PHP基于MySQLI函數封裝的數據庫連接工具類【定義與用法】
  • 常用PHP封裝分頁工具類
  • php封裝的驗證碼工具類完整實例
  • PHP封裝的驗證碼工具類定義與用法示例
  • php封裝的pdo數據庫操作工具類與用法示例
  • PHP抓取、分析國內視頻網站的視頻信息工具類
  • PHP常用工具類大全附全部代碼下載
  • PHP實現基于面向對象的mysqli擴展庫增刪改查操作工具類
  • PHP實現可添加水印與生成縮略圖的圖片處理工具類
  • php實現網頁緩存的工具類分享

標簽:銅陵 衡陽 鷹潭 重慶 十堰 巴彥淖爾 臨沂 麗江

巨人網絡通訊聲明:本文標題《PHP常用的類封裝小結【4個工具類】》,本文關鍵詞  PHP,常用的,常,用的,類,封裝,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《PHP常用的類封裝小結【4個工具類】》相關的同類信息!
  • 本頁收集關于PHP常用的類封裝小結【4個工具類】的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 少妇喷潮????张京| 久久久久久久网| 国产精品秘?入口久久熟女| 国产东北人一级A片大全| 含一整夜好涨h| 天美传媒一区二区三区| 公啊?好痛?嗯?轻一点黄| 国产亚洲午夜精品一区二区在线| 农村妇女肥大毛多水多| footstockings欧美丝袜| 娇女的呻吟亲女禁忌h16| 人与牲囗性恔配视频L| 免费人成网ww44kk44| 国产激情精品一区二区三区| 最近韩国动漫免费观看| 和寡妇做受不了视频| 扣扣文化传媒| 一级做A爱片久久毛片老师机| 韩国理伦片OK电影天堂| 公交车上扒开裙子进入| 日本Av亚洲Av欧洲Av| 你的奶好大让老子摸摸的说说| 女人下面毛多又黑又厚| 毛亚美女?黄?免费网站| 天堂亚洲精品少妇毛无码| 和闺蜜一起三飞18p| 小小的日本高清在线观看| 人人爱天天做夜夜爽2020麻豆| 《美国式禁忌2 愈演愈烈》| 天海翼免费高清在线观看| 艹逼视频软件下载| 免费性生活| 扒开腿狂躁女人爽出白浆A片漫画 国产又粗又猛又爽又黄的视频一区 | 成人黄色三级| 91丝瓜轻量版和正式版| MM131美女大尺度私房照尤果| 永久免费看黄漫画软件| 我的a级秘密| 国产成人av在线| 欧美精18videosex性欧美| 女人18与19毛片免费|