批改状态:合格
老师批语:希望你能完全理解写的代码
写一个抽象类并继承它, 内容自定
模仿课堂案例,写一个接口实现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>';点击 "运行实例" 按钮查看在线实例

写一个抽象类并继承它, 内容自定
抽象类和方法就是在前面加上关键字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();点击 "运行实例" 按钮查看在线实例

模仿课堂案例,写一个接口实现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函数去掉。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号