登录  /  注册
博主信息
博文 34
粉丝 0
评论 0
访问量 22068
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
第15章 抽象类与接口实战-2019年10月09日20时00分
Tommy-黄天浩的博客
原创
558人浏览过
  1. 写一个抽象类并继承它, 内容自定。

实例

<?php

namespace _001;

abstract class Position1
{
    protected $position;

    public function __construct($position){
        $this->position=$position;
    }

    public function GetPosition(){
        return $this->position;
    }

    abstract public function SetPosition($value);
}

class Position2 extends Position1{
    //构造方法不会继承
    public function __construct($position='基层人员'){
        parent::__construct($position);
    }

    //抽象类中定义的抽象方法必须在子类中实现
    public function SetPosition($value){
        $this->position=$value;
    }
}

// 注意
//1. 抽象类不能实例化
//2. 抽象类中定义的抽象方法必须在子类中实现

$obj=new Position2('办公室主任');
echo $obj->GetPosition();

运行实例 »

点击 "运行实例" 按钮查看在线实例

运行结果如图所示:

QQ截图20191011211043.png


2. 模仿课堂案例,写一个接口实现CURD操作, 并扩展一到二个方法。

 

实例

<?php

namespace _001;

interface iCurd{
    //创建数据
    public function create($data);

    //删除数据
    public function delete($where);

    //查询数据
    public function read();

    //更新数据
    public function update($data,$where);

    //查询某个关键词的数据
    public function find($fields,$ziduan,$like,$limit);
}

class Db implements iCurd{

    //数据库连接对象
    protected $pdo=null;

    //数据表
    protected $table;

    //构造方法,连接数据库
    public function __construct($dsn,$user,$password,$table='staff'){
        $this->pdo=new \PDO($dsn,$user,$password);
        $this->table=$table;
    }

    //创建数据
    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;
        $stmt=$this->pdo->prepare($sql);
        $stmt->execute($data);

        return [
            'count' =>$stmt->rowcount(),
            'id' =>$this->pdo->lastinsertid()
        ];
    }

    //删除数据
    public function delete($where)
    {
        $sql = 'DELETE FROM ' .$this->table . ' WHERE ' .$where;
        $stmt = $this->pdo->prepare($sql);
        $stmt->execute();

        return $stmt->rowCount();
    }

    //查询数据
    public function read($fields='*', $where='', $limit='0, 5')
    {
        //设置条件
        $where = empty($where) ? '' : ' WHERE ' . $where;

        $sql='SELECT '.$fields.' FROM '.$this->table.$where.' LIMIT '.$limit;
        $stmt=$this->pdo->prepare($sql);
        $stmt->execute();

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

    //更新数据
    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;
        // die($sql);
        $stmt=$this->pdo->prepare($sql);
        $stmt->execute($data);

        return $stmt->rowCount();
    }

    //查询某个关键词的数据
    public function find($fields='*',$ziduan,$like,$limit='5')
    {
        $sql='SELECT '.$fields.' FROM '.$this->table.' WHERE '.$ziduan.' LIKE '."'".'%'.$like.'%'."'".' LIMIT '.$limit;
        // die($sql);
        $stmt=$this->pdo->prepare($sql);
        $stmt->execute();

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

//客户端代码
$dsn ='mysql:host=127.0.0.1;dbname=php';
$user='root';
$password='root';
$db=new DB($dsn,$user,$password);

// 新增
// $data = [
//    'name' => '郭靖',
//    'age' => 29,
//    'sex' => 1,
//    'position' => '金刀驸马',
//    'mobile' => '1389998899',
//    'hiredate' => time()
// ];

// $res=$db->create($data);
// echo '新增了'.$res['count'].'条记录,新增这条记录的主键ID是:'.$res['id'].'<br>';

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

//更新数据
// $data = [
//    'age' => 40,
//    'position' => '抗金英雄'
// ];

// $where = 'staff_id = 11';
// echo '成功的更新了: ' . $db->update($data,$where) . ' 条记录';

//删除数据

// $where='staff_id = 11';
// echo '成功的删除了:'.$db->delete($where).'条记录';

//查询关键词
print_r($db->find('*','name','亮','5'));

运行实例 »

点击 "运行实例" 按钮查看在线实例

运行效果如下图所示:

QQ截图20191011211043.png

批改状态:合格

老师批语:find()方法写得不错
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学