1. 抽象类
抽象类可以有抽象方法,也可以有普通方法
abstract class operate{
public $a;
public $b;
//$a与$b注释掉也没问题
// public $sum;
// public $difference;
public function __construct($a,$b){
$this->a = $a;
$this ->b = $b;
}//如果需要给类的实例最初赋值,必须要用构造方法
abstract public function sum();
abstract public function difference();
}
class operate_detail extends operate{
public function sum(){
return $this->a+$this->b;
}
public function difference(){
return $this->a-$this->b;
}
}
// $o = new operate(100,50);
// Cannot instantiate 无法实例化
$o_d =new operate_detail(100,50);
var_dump($o_d->sum());
echo '<hr>';
echo $o_d->difference();
2. 接口
接口是制定规则,只有抽象函数和常量
interface operate2{
const PRODUCT = '积为:';
const QUOTIENT = '商为:';
public function result($c,$d);
//注意这个地方没有abstract因为interface默认了方法就是抽象的
}
class operate2_detail implements operate2{
public function result($c, $d)
{
$result1 = $c*$d;
$result2 = $c/$d;
return $c.'与'.$d.' '.self::PRODUCT.$result1.' --- '.self::QUOTIENT.$result2;
//常量调用要用self::
// return self::PRODUCT.$result1 . '---' . self::QUOTIENT.$result2; '---'前后用不用空格都一样,''里能体现空格
// TODO: Implement result() method.
}
}
$o_d2 = new operate2_detail();
echo $o_d2->result(100,50);
1,2题运行结果
3. 上次的作业写好
按照接口的方式来写,将其简化为4个操作方法:create(insert)、read(select)、update、delete。
interface curd{
public function creat($data);
public function update($data,$where);
public function read($fields, $where, $order, $limit);
public function delete($where);
}
class DB2 implements curd
{
public $pdo;
public $table;
public function __construct($dsn, $user, $password, $table)
{
$this->table = $table;
$this->pdo = new PDO($dsn, $user, $password);
//已经预处理,后面函数不用添加此语句了
}
public function read($fields, $where = '', $order = '', $limit = '')
{
// $this->table = $table;
$sql = 'SELECT ';
if (is_array($fields)) {
foreach ($fields as $field) {
$sql .= $field . ', ';
}
} else {
$sql .= $fields;
}
$sql = rtrim(trim($sql), ',');
$sql .= ' FROM ' . $this->table;
//查询条件
if (!empty($where)) {
$sql .= ' WHERE ' . $where;
}
//排序条件
if (!empty($order)) {
$sql .= ' order by ' . $order;
}
//分页条件
if (!empty($limit)) {
$sql .= ' limit ' . $limit;
}
$sql .= ';';
//创建PDO预处理对象
$stmt = $this->pdo->prepare($sql);
//执行查询操作
if ($stmt->execute()) {
if ($stmt->rowCount() > 0) {
$stmt->setFetchMode(PDO::FETCH_ASSOC);
//返回一个二维数组
return $stmt->fetchAll();
}
} else {
return false;
}
}
public function creat($data = [])
{
$sql = "INSERT INTO {$this->table} SET ";
//组装插入语句
if (is_array($data)) {
foreach ($data as $k => $v) {
$sql .= $k . '="' . $v . '", ';
}
} else {
return false;
}
//去掉尾部逗号,并添加分号结束
$sql = rtrim(trim($sql), ',') . ';';
//创建PDO预处理对象
$stmt = $this->pdo->prepare($sql);
//执行新增操作
if ($stmt->execute()) {
if ($stmt->rowCount() > 0) {
return true;
}
} else {
return false;
}
}
public function update($data = [], $where = '')
{
$sql = "UPDATE {$this->table} SET ";
//组装修改语句
if (is_array($data)) {
foreach ($data as $k => $v) {
$sql .= $k . '="' . $v . '", ';
}
}
//去掉尾部逗号,并添加分号结束
$sql = rtrim(trim($sql), ',');
//查询条件
if (!empty($where)) {
$sql .= ' WHERE ' . $where;
}
//创建PDO预处理对象
$stmt = $this->pdo->prepare($sql);
//执行新增操作
if ($stmt->execute()) {
if ($stmt->rowCount() > 0) {
return true;
}
} else {
return false;
}
}
public function delete($where = '')
{
$sql = "DELETE FROM {$table} ";
//查询条件
if (!empty($where)) {
$sql .= ' WHERE ' . $where;
}
//创建PDO预处理对象
$stmt = $this->pdo->prepare($sql);
//执行删除操作
if ($stmt->execute()) {
if ($stmt->rowCount() > 0) {
return true;
}
} else {
return false;
}
}
}
$db2 = new DB2('mysql:host=localhost;dbname=anguoguo','root','root','movies');
//连接pdo
print_r($db2);
print_r($db2->read('name','cate_id=2','mov_id DESC')) ;
echo '<hr/>';
$array3 =[
'mov_id' =>10,
'name' =>'插入',
'image'=>'10.jpg',
'detail'=>'能否插入成功',
'cate_id'=>'4'
];
print_r($db2->creat('$array3'));
echo '<hr/>';
$array4 =['image'=>'101.jpg'];
print_r($db2->update($array4,'mov_id=5'));
echo '<hr/>';
print_r( $db2->delete(''));
echo '<hr/>';
$array5 =['image'=>'5.jpg'];
print_r($db2->update($array5,'mov_id=5'));
echo '<hr/>';
结果为: