博主信息
博文 27
粉丝 1
评论 0
访问量 28173
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
抽象类与接口实战-2019年10月9日
思杰的博客
原创
843人浏览过
  1. 写一个抽象类并继承它, 内容自定

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


课堂笔记:

类的自动加载技术,就是通过php中的一个方法spl_autoload_register()

实例

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>';

运行实例 »

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

11.png


写一个抽象类并继承它, 内容自定

        抽象类和方法就是在前面加上关键字abstract,抽象方法就是不用具体实现的方法,只有一个类中有一个抽象方法,那么该类就需要改成抽象类。抽象方法在子类中必须要实现。父类的构造方法在子类中不会被继承,所以要重新写

实例

<?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中文网的创始人是'.$person->getName();
echo '<br>';
$person->setName('朱老师');
echo 'php中文网的讲师是'.$person->getName();

运行实例 »

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

22.png


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

类是对象的模版,接口就是类的模版,接口是用来定义里面的方法和标准,让类去具体实现他。用implements去实现他。

实例

<?php
namespace _1009;

//接口开发实战

interface iCurd{
    //增加数据
    public function create($data);

    //读取数据
    public function read();

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

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

//创建一个db类,实现iCurd接口,完成基本的数据库操作

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;
//        die($sql);
        $stmt = $this->pdo->prepare($sql);
        $stmt->execute($data);
        return [
            'count'=>$stmt->rowCount(),
        'id'=>$this->pdo->lastInsertId()
        ];
    }

    //读取数据
    public function read($fields='*',$where='',$limit='6'){

        //设置条件
        $where = empty($where)?'' : 'where' .$where;

        //显示数量
        $limit = ' limit '.$limit;

        $sql = 'select '.$fields.' from '.$this->table.$where.$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;
        echo $sql;
        $stmt = $this->pdo->prepare($sql);
        $stmt->execute($data);
        return $stmt->rowCount();
    }

    //删除数据
    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>';
}

//新增数据
//$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>';

//更新数据
//
//$data=[
//    'age'=>40,
//    'position'=>'抗金英雄'
//];
//$where = 'staff_id = 6';
//echo '成功更新了'.$db->update($data,$where).'条记录';

//删除数据
$where = 'staff_id = 6';
echo '成功删除了'.$db->delete($where).'条记录';

运行实例 »

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

主要是更新数据那里,需要使用array_keys($data)这个函数去获取数组的键值,还有就是条件右边的逗号用rtrim函数去掉。

批改状态:合格

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

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

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