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

搜索
博主信息
博文 27
粉絲 1
評論 0
訪問量 27614
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板
抽象類與接口實(shí)戰(zhàn)-2019年10月9日
思杰的博客
原創(chuàng)
827人瀏覽過
  1. 寫一個(gè)抽象類并繼承它, 內(nèi)容自定

  2. 模仿課堂案例,寫一個(gè)接口實(shí)現(xiàn)CURD操作, 并擴(kuò)展一到二個(gè)方法


課堂筆記:

類的自動(dòng)加載技術(shù),就是通過php中的一個(gè)方法spl_autoload_register()

實(shí)例

spl_autoload_register(function ($className){
    $path = str_replace('\\',DIRECTORY_SEPARATOR,$className);
    $path =  __DIR__.'/'.$path.'.php';
    if(file_exists($path)) include $path;
});

echo \inc\Test1::get();
echo '<br>';
echo \inc\Test2::get();
echo '<br>';
echo \inc\Test3::get();
echo '<br>';
echo \inc\Test4::get();
echo '<br>';

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

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

11.png


寫一個(gè)抽象類并繼承它, 內(nèi)容自定

        抽象類和方法就是在前面加上關(guān)鍵字abstract,抽象方法就是不用具體實(shí)現(xiàn)的方法,只有一個(gè)類中有一個(gè)抽象方法,那么該類就需要改成抽象類。抽象方法在子類中必須要實(shí)現(xiàn)。父類的構(gòu)造方法在子類中不會(huì)被繼承,所以要重新寫

實(shí)例

<?php
abstract class Person2{
    protected $name;
    public function __construct($name = 'peter zhu')
    {
        $this->name = $name;
    }

    public function getName(){
        return $this->name;
    }

    abstract public function setName($value);
}

class Person3 extends Person2{

    public function __construct($name)
    {
        parent::__construct($name);
    }

    public function setName($value)
    {
        $this->name = $value;
    }
}

$person = new Person3('豬哥');
echo 'php中文網(wǎng)的創(chuàng)始人是'.$person->getName();
echo '<br>';
$person->setName('朱老師');
echo 'php中文網(wǎng)的講師是'.$person->getName();

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

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

22.png


模仿課堂案例,寫一個(gè)接口實(shí)現(xiàn)CURD操作, 并擴(kuò)展一到二個(gè)方法

類是對象的模版,接口就是類的模版,接口是用來定義里面的方法和標(biāo)準(zhǔn),讓類去具體實(shí)現(xiàn)他。用implements去實(shí)現(xiàn)他。

實(shí)例

<?php
namespace _1009;

//接口開發(fā)實(shí)戰(zhàn)

interface iCurd{
    //增加數(shù)據(jù)
    public function create($data);

    //讀取數(shù)據(jù)
    public function read();

    //更新數(shù)據(jù)
    public function update($data,$where);

    //刪除數(shù)據(jù)
    public function delete($where);
}

//創(chuàng)建一個(gè)db類,實(shí)現(xiàn)iCurd接口,完成基本的數(shù)據(jù)庫操作

class Db implements iCurd{
    //數(shù)據(jù)庫連接對象
    protected $pdo = null;

    //數(shù)據(jù)表
    protected $table;

    //構(gòu)造方法,實(shí)例化的時(shí)候連接數(shù)據(jù)庫,并設(shè)置默認(rèn)的數(shù)據(jù)表名稱
    public function __construct($dsn,$user,$password,$table ='staff')
    {
        $this->pdo = new \PDO($dsn,$user,$password);
        $this->table = $table;
    }

    //增加數(shù)據(jù)
    public function create($data){
        $fields = ' (name,age,sex,position,mobile,hiredate) ';

        $values = ' (:name,:age,:sex,:position,:mobile,:hiredate)';

        $sql = 'insert into '.$this->table.$fields.' values '.$values;
//        die($sql);
        $stmt = $this->pdo->prepare($sql);
        $stmt->execute($data);
        return [
            'count'=>$stmt->rowCount(),
        'id'=>$this->pdo->lastInsertId()
        ];
    }

    //讀取數(shù)據(jù)
    public function read($fields='*',$where='',$limit='6'){

        //設(shè)置條件
        $where = empty($where)?'' : 'where' .$where;

        //顯示數(shù)量
        $limit = ' limit '.$limit;

        $sql = 'select '.$fields.' from '.$this->table.$where.$limit;
        $stmt = $this->pdo->prepare($sql);
        $stmt->execute();

        return $stmt->fetchAll(\PDO::FETCH_ASSOC);
    }

    //更新數(shù)據(jù)
    public function update($data,$where){
        $keyArr = array_keys($data);
        $set = '';
        foreach ($keyArr as $value){
            $set.= $value . ' =:'.$value.', ';
        }

        $set= rtrim($set,', ');

        $sql = 'update '.$this->table. ' set '.$set.' where '.$where;
        echo $sql;
        $stmt = $this->pdo->prepare($sql);
        $stmt->execute($data);
        return $stmt->rowCount();
    }

    //刪除數(shù)據(jù)
    public function delete($where){
        $sql = 'delete from '.$this->table.' where '.$where;
        $stmt = $this->pdo->prepare($sql);
        $stmt->execute();
        return $stmt->rowCount();
    }
}


//客戶端代碼
$dsn = 'mysql:host=127.0.0.1;dbname=huangsijie';
$user ='root';
$password = 'root';
$db = new Db($dsn,$user,$password);

foreach ($db->read()as $item ){
    print_r($item);
    echo '<br>';
}

//新增數(shù)據(jù)
//$data = [
//    'name'=>'郭大俠',
//    'age'=>29,
//    'sex'=>1,
//    'position'=>'金刀駙馬',
//    'mobile'=>'1888888888',
//    'hiredate'=>123123
//];
//
//$res = $db->create($data);
//echo '成功的新增了'.$res['count'].'條記錄,新增的主鍵ID是'.$res['id'];
//echo '<br>';
//echo '<br>';

//更新數(shù)據(jù)
//
//$data=[
//    'age'=>40,
//    'position'=>'抗金英雄'
//];
//$where = 'staff_id = 6';
//echo '成功更新了'.$db->update($data,$where).'條記錄';

//刪除數(shù)據(jù)
$where = 'staff_id = 6';
echo '成功刪除了'.$db->delete($where).'條記錄';

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

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

主要是更新數(shù)據(jù)那里,需要使用array_keys($data)這個(gè)函數(shù)去獲取數(shù)組的鍵值,還有就是條件右邊的逗號用rtrim函數(shù)去掉。

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

老師批語:希望你能完全理解寫的代碼
本博文版權(quán)歸博主所有,轉(zhuǎn)載請注明地址!如有侵權(quán)、違法,請聯(lián)系admin@php.cn舉報(bào)處理!
全部評論 文明上網(wǎng)理性發(fā)言,請遵守新聞評論服務(wù)協(xié)議
0條評論
關(guān)于我們 免責(zé)申明 意見反饋 講師合作 廣告合作 最新更新
php中文網(wǎng):公益在線php培訓(xùn),幫助PHP學(xué)習(xí)者快速成長!
關(guān)注服務(wù)號 技術(shù)交流群
PHP中文網(wǎng)訂閱號
每天精選資源文章推送
PHP中文網(wǎng)APP
隨時(shí)隨地碎片化學(xué)習(xí)
PHP中文網(wǎng)抖音號
發(fā)現(xiàn)有趣的

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

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