亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

搜索
博主信息
博文 34
粉絲 1
評(píng)論 1
訪問(wèn)量 47225
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板
利用接口實(shí)現(xiàn)一個(gè)基本數(shù)據(jù)庫(kù)的操作類(CURD)——2019年8月2日22時(shí)03分
嘿哈的博客
原創(chuàng)
1015人瀏覽過(guò)

類的自動(dòng)加載

str_replace() 字符串替換語(yǔ)法

sql_autoload_register( function(){} ) 自動(dòng)加載函數(shù)

file_exists() 確定文件是否存在


抽象類與抽象方法:

abstract 抽象關(guān)鍵字;

抽象類不能實(shí)例化,類中抽象方法必須在子類全部實(shí)現(xiàn);

abstract public function setName ($value); 抽象方法

在子類 創(chuàng)建public function setName($value); 實(shí)現(xiàn)父類的抽象方法

接口原理與實(shí)現(xiàn)

interface {} 定義一個(gè)接口;

implement 類實(shí)現(xiàn)接口的關(guān)鍵字

注意:

1.接口不能實(shí)例化;

2.接口中只允許出現(xiàn)抽象方法;

3.接口中成員必須是公共/public;

4.允許有常量;

5.實(shí)現(xiàn)接口的子類,必須將接口的抽象方法全部實(shí)現(xiàn);


類的自動(dòng)加載

class Loader
{
    public static function autoLoader()
    {
        sql_autoload_register(function($className){
            $path = str_replace('\\','/',$className);
            $path = __DIR__ . '/' . $path . 'php';
            if(file_exists($path)){
                require $path ; 
            }
        });
    }
}


實(shí)例

<?php

    namespace demo;

    //定義一個(gè)接口
    interface iCurd
    {
        //增加
        public function create($data);
        //查詢
        public function read();
        //設(shè)置
        public function update($data,$where);
        //刪除
        public function delete($where);

    }

    class sub implements iCurd
    {
        //連接對(duì)象
        protected $pdo=null;

        protected $table='staff';
        //連接數(shù)據(jù)庫(kù)
        public function __construct($dsn,$username,$password,$table)
        {
            $this->pdo = new \PDO($dsn,$username,$password);
            $this->table = $table;
        }

        //增加方法
        public function create($data)
        {
            //字段
            $fields = ' (name,age,sex,position,mobile,hiredate) ';
            //值
            $values = ' (:name, :age, :sex, :position, :mobile, :hiredate) ';
            //sql語(yǔ)句
            $sql = 'INSERT INTO '.$this->table . $fields . ' VALUES ' . $values ;
            //sql預(yù)處理
            $stmt = $this->pdo->prepare($sql);
//            die($stmt->debugDumpParams());
            $stmt->execute($data);

            //返回新增數(shù)據(jù)
            return [
                'count' => $stmt->rowCount(),
                'id' => $this->pdo->lastInsertId()
            ];

        }
        //查詢方法
        public function read($fields = '*',$where = '',$limit = '0, 5')
        {
            $where = empty($where) ? $where : ' WHERE '.$where;
            $limit = ' LIMIT '.$limit;
            $sql = 'SELECT '.$fields . ' FROM '.$this->table . $where . $limit;

            $stmt = $this->pdo->prepare($sql);
            $stmt -> execute();
//            die($stmt->debugDumpParams());
            return $stmt->fetchAll(\PDO::FETCH_ASSOC);
        }
        //更新方法
        public function update($data,$where)
        {
            // 獲取$data的鍵值
            $keyArr = array_keys($data);
            $set = '';

            foreach ($keyArr as $value){
                $set .= $value .'= :'.$value.',';
            }
            //去除右邊的逗號(hào)
            $set = rtrim($set,',');
            //sql語(yǔ)句
            $sql = 'UPDATE ' .$this->table .' SET '.$set.' WHERE '.$where;
            //預(yù)處理
            $stmt = $this->pdo->prepare($sql);
            $stmt->execute($data);
//            die($stmt->debugDumpParams());
            return $stmt->rowCount();
        }
        //刪除方法
        public function delete($where)
        {
            $sql = 'DELETE FROM '.$this->table .' WHERE '. $where;
            $stmt = $this->pdo->prepare($sql);
            $stmt->execute();

            return $stmt->rowCount();
        }

    }

    //客戶端代碼
    //實(shí)例化數(shù)據(jù)庫(kù)
    $dsn = 'mysql:host=127.0.0.1;dbname=php';
    $password = 'root';
    $username = 'root';
    $table = 'staff'; //表名
    $db = new sub($dsn,$username,$password,$table);

    //導(dǎo)入數(shù)據(jù)
    $data = [

        'name'=> '洪吉潮',
        'age'=> 18,
        'sex' => 1,
        'position' => '主席',
        'mobile'=> '15626475734',
        'hiredate' => time()

    ];
    //增加執(zhí)行結(jié)果
//    $res = $db->create($data);
//    echo '成功新增 '. $res['count']. '條記錄, 新增記錄ID是: '. $res['id'];
//    echo '<hr>';

    //查詢執(zhí)行結(jié)果
    foreach ($db->read('name,age,position','staff_id>5','5')as $item ){
     print_r($item);
    echo '<br>';
    }
    echo '<hr>';
    //更新執(zhí)行結(jié)果
    $data = [
        'name' => '李文茜'
    ];
    $where = 'staff_id=12';
    echo '成功更新'.$db->update($data,$where).'條記錄';
    echo '<hr>';
    //刪除執(zhí)行結(jié)果
    $where = 'staff_id=11';
    echo '成功刪除'.$db->delete($where).'條記錄';

運(yùn)行實(shí)例 ?

點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例



批改狀態(tài):合格

老師批語(yǔ):總結(jié)的非常好, 接口才是以后開(kāi)發(fā)最常用的, 抽象類只是接口的一個(gè)實(shí)例罷了
本博文版權(quán)歸博主所有,轉(zhuǎn)載請(qǐng)注明地址!如有侵權(quán)、違法,請(qǐng)聯(lián)系admin@php.cn舉報(bào)處理!
全部評(píng)論 文明上網(wǎng)理性發(fā)言,請(qǐng)遵守新聞評(píng)論服務(wù)協(xié)議
0條評(píng)論
關(guān)于我們 免責(zé)申明 意見(jiàn)反饋 講師合作 廣告合作 最新更新
php中文網(wǎng):公益在線php培訓(xùn),幫助PHP學(xué)習(xí)者快速成長(zhǎng)!
關(guān)注服務(wù)號(hào) 技術(shù)交流群
PHP中文網(wǎng)訂閱號(hào)
每天精選資源文章推送
PHP中文網(wǎng)APP
隨時(shí)隨地碎片化學(xué)習(xí)
PHP中文網(wǎng)抖音號(hào)
發(fā)現(xiàn)有趣的

Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號(hào)

  • 登錄PHP中文網(wǎng),和優(yōu)秀的人一起學(xué)習(xí)!
    全站2000+教程免費(fèi)學(xué)