批改状态:合格
老师批语:接口与抽象类很像, 使用的时候, 大多数情况下, 的确是可以互换的, 所以一定要多多留意
一、抽象类的继承
<?php
namespace _1009;
abstract class Animal {
public $name;
public $color;
public $weight;
public $width;
public $height;
public function __construct ($name, $color, $weight, $width, $height) {
$this->name = $name;
$this->color = $color;
$this->weight = $weight;
$this->width = $width;
$this->height = $height;
}
abstract public function eat();
abstract public function drink();
abstract public function run();
}
class Cat extends Animal {
public function __construct ($name='小花猫', $color='白色', $weight='10kg', $width='20公分', $height='10公分') {
parent::__construct($name, $color, $weight, $width, $height);
$this->name = $name;
$this->color = $color;
$this->weight = $weight;
$this->width = $width;
$this->height = $height;
}
public function eat () {
echo 'This is eat';
}
public function drink () {
echo 'This is drink';
}
public function run () {
echo 'This is run';
}
public function getInfo () {
echo '我的名字是'.$this->name.', 颜色:'.$this->color.' 体重:'.$this->weight.' 宽度:'.$this->width.' 身高'.$this->height;
}
}
$obj = new Cat();
$obj->getInfo();点击 "运行实例" 按钮查看在线实例
二、接口实现案例
<?php
interface iCurd {
//添加
public function add($data);
//删除
public function del($where);
//修改
public function update($data, $where);
//查询
public function select();
//获取get post 请求参数
public function input($str);
}
//数据库类
class Db implements iCurd {
//数据库连接对象
protected $pdo = null;
//连接的数据表
protected $table;
//请求类型
protected $type;
//请求参数
protected $param;
public function __construct($dsn, $username, $password, $table='staff'){
$this->pdo = new \PDO($dsn, $username, $password);
$this->table = $table;
}
//添加
public function add($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 del($where){
$sql = 'delete from '.$this->table.' where '.$where;
$stmt = $this->pdo->prepare($sql);
$stmt->execute();
return $stmt->rowCount();
}
//修改
public function update($data, $where){
$keys = array_keys($data);
$set = '';
foreach($keys as $value){
$set .= $value.'='.':'.$value.',';
}
$set = rtrim($set, ',');
$sql = 'update '.$this->table.' set '.$set.' where '.$where;
$stmt = $this->pdo->prepare($sql);
$stmt->execute();
return $stmt->rowCount();
}
//查询
public function select($fields='*', $where='', $limit='0, 5'){
$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::FETCHA_ASSOC);
}
//字符串处理
public function setStr($str){
$str_arr = explode('.' ,strtolower(trim($str)));
$this->type = $str_arr[0];
$this->param = $str_arr[1];
}
//获取请求参数
public function input($str){
$this->setStr($str);
if($this->type=='get'){
return $_GET["$this->param"];
}
if($this->type=='post'){
return $_POST["$this->param"];
}
}
}
//数据库实例
$dsn = "mysql:localhost=127.0.0.1;dbname=phpcncms";
$username = 'root';
$password = '';
$obj = new Db($dsn, $username, $password);
$id = $obj->input('get.id');点击 "运行实例" 按钮查看在线实例
总结:
1 在继承抽象类时,子类中要手动实现构造方法,因为子类不会自动继承抽象类的构造方法
2 接口要使用interface关键字,接口中方法不能有实体,即抽象方法。在实现接口时使用implements关键字,接口中的抽象方法都要被实现。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号