博主信息
博文 18
粉丝 0
评论 0
访问量 16540
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
类的自动加载、抽象类、接口类-2019-10-09
无聊了的博客
原创
762人浏览过

1、写个抽象类并继承它

实例

<?php

abstract class Car
{
    abstract protected function price($price);
    abstract protected function brand($brand);
    abstract protected function type($type);
}

class DZCar
{
    protected function price($price){
        return '汽车价格:'.$price;
    }
    protected function brand($brand){
        return '汽车***:'.$brand;
    }
    protected function type($type){
        return '汽车类型:'.$type;
    }

    public function get($brand,$price,$type){
        return $this->brand($brand) . '<br>' .$this->price($price) . '<br>' . $this->type($type);
    }
}

echo (new DZCar())->get('大众','20W','汽油');

运行实例 »

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

2、通过接口实现CURD操作,并扩展一到二个方法

实例

实例

<?php
interface Db
{
    function select($field='*',$where='',$limit='0,5');
    function update($data,$where=[]);
    function add($data);
    function del($where = []);
    function delBatch($where = []);
}

class Query implements Db
{
    protected $pdo = null;

    protected $table;

    public function __construct($dsn,$use,$pass,$table = 'demo')
    {
        $this->pdo = new \PDO($dsn,$use,$pass);
        $this->table = $table;
    }

    function select($field='*',$where='',$limit='0,5'){
        $where = empty($where) ? '' : ' where ' . $where;
        $limit = ' limit '. $limit;
        $sql = 'select '. $field .' from ' . $this->table . $where . $limit;
        $smtp = $this->pdo->prepare($sql);
        $smtp->execute();
        return $smtp->fetchAll(\PDO::FETCH_ASSOC);
    }

    function update($data,$where = [])
    {
        if(count($where) == 0){
            return '条件不能为空';
        }
        $mkey = array_merge($data,$where);
        $key = array_keys($data);
        $wkey = array_keys($where);
        $value = '';
        foreach($key as $val){
            $value .= $val .'=:' . $val . ',';
        }
        $value = rtrim($value,',');
        $where= '';
        foreach($wkey as $w){
            $where .=   ' and ' . $w .'=:' . $w ;
        }
        $where = ltrim($where,' and ');
        $sql = 'update ' . $this->table . ' set ' . $value . ' where ' . $where;
        // echo $sql;
        $smtp = $this->pdo->prepare($sql);
        $smtp->execute($mkey);
        return $smtp->rowCount();
    }

    function add($data)
    {
        $key = "(name,age,addr)";
        $value = "(:name,:age,:addr)";
        $sql = 'insert into ' . $this->table . $key . 'values' . $value;
        // echo $sql;
        $smtp = $this->pdo->prepare($sql);
        $smtp->execute($data);
        return $data = [
            "count" => $smtp->rowCount(),
            "id" => $this->pdo->lastInsertId()
        ];
    }

    function del($where = [])
    {
        if(count($where) == 0){
            return '条件不能为空';
        }
        $wkey = array_keys($where);
        $wval = '';
        foreach($wkey as $w){
            $wval .= ' and ' . $w . '=:' . $w;
        }
        $wval = ltrim($wval,' and ');
        $sql = 'delete from ' . $this->table . ' where ' . $wval;
        // echo $sql;
        $smtp = $this->pdo->prepare($sql);
        $smtp->execute($where);
        return $smtp->rowCount();
    }

    function delBatch($where = []){
        if(count($where) == 0){
            return '条件不能为空';
        }
        $wval = '';
        $newWhere = [];
        foreach($where as $key=>$val){
            $wval .= '?,';
            $newWhere[$key+1] = $val;
        }
        $wval = trim($wval,',');
        $sql = 'delete from ' . $this->table . ' where id in (' . $wval . ')';
        // echo $sql;
        $smtp = $this->pdo->prepare($sql);
        foreach($newWhere as $key=>$val){
            $smtp->bindValue($key,$val);
        }
        $smtp->execute();
        return $smtp->rowCount();
    }
}
$dsn = 'mysql:host=127.0.0.1;dbname=test';
$use = 'root';
$pass = '000000';
$obj = new Query($dsn,$use,$pass,'testInfo');
//查询
echo '<pre>';
print_r($obj->select());
echo '<hr>';
//更新

$data = [
    "name" => '小红',
    "addr" => '北京'
];
$where = [
    'id' => '3',
    'age' => '18'
];
print_r($obj->update($data,$where));
echo '<hr>';

//删除
$where = [
    'id' => '6',
    // 'age' => '18'
];
print_r($obj->del($where));
echo '<hr>';
//添加
$data = [
    "name" => '1',
    "age" => '22',
    "addr" => '33'
];

print_r($obj->add($data));
echo '<hr>';
//批量删除

$where = [7,8,9,10];
print_r($obj->delBatch($where));

运行实例 »

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

总结:

1、抽象类内部方法可以抽象也可以是普通方法。抽象方法没有实体,需要子类去继承实现,不能是私有的,不能被实例化

2、接口类内部方法都为接口类型,必须是公共的,子类必须继承实现,可以实现多继承

批改状态:合格

老师批语:接口中的public关键定, 还是要写的, 建议不要省略掉
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系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+教程免费学