批改状态:合格
老师批语:这些基础知识 , 先掌握原理与实现, 具体场景在实战中学习
第一个知识点,是类的自动加载,使用函数spl_autoload_register()自动加载类文件,主要是使用了PHP的两个函数,演示如下
<?php
namespace __1009;
//类的自动加载,使用函数spl_autoload_register()自动加载类文件。
spl_autoload_register(function($className){
$path=str_replace('\\',DIRECTORY_SEPARATOR,$className);
$path=__DIR__.DIRECTORY_SEPARATOR.$path.'.php';
include $path;
});点击 "运行实例" 按钮查看在线实例
第二个知识点,是抽象类,抽象类声明一些属性和方法,由子类来继承来实现这些方法,抽象类和抽象方法由关键字abstract声明。抽象类不能实例化,抽象类中定义的抽象方法必须在子类中实现。
作业:写一个抽象类并继承它, 内容自定
<?php
abstract class demo {
protected $name;
protected $age;
protected $sex;
abstract public function getName();
public function __construct($name,$age,$sex)
{
$this->name=$name;
$this->age=$age;
$this->sex=$sex;
}
}
class demo1 extends demo {
public function __construct($name, $age, $sex)
{
parent::__construct($name, $age, $sex);
}
//获取名字
public function getName(){
return $this->name;
}
//获取年龄
public function getAge(){
return $this->age;
}
//获取性别
public function getSex(){
return $this->sex;
}
//
public function setSex($value){
$this->sex=$value;
}
}
$obj=new demo1('保罗',22,'男');
echo $obj->getName().'的年龄是'.$obj->getAge().'岁,他是一个'.$obj->getSex().'的。';
$obj->setsex('女');
echo '但是,就在昨天他去了一趟泰国,于是就变成了'.$obj->getSex().'的,愿上帝保佑她。';点击 "运行实例" 按钮查看在线实例
第三个知识点是,接口,接口是类的模板,类是接口的实现,接口内的方法都是抽象方法,接口用interface 来进行定义声明,类用implements来实现接口。
作业:模仿课堂案例,写一个接口实现CURD操作, 并扩展一到二个方法
<?php
namespace __1009;
interface iCurd{
public function create($data);
public function read();
public function update($data,$where);
public function delete($where);
}
class DB implements iCurd
{
protected $pdo=null;
protected $table;
public function __construct($dsn,$user,$pass,$table='user')
{
$this->pdo=new \PDO($dsn,$user,$pass);
$this->table=$table;
}
//增加数据
public function create($data)
{
//设置数据绑定格式
$fields='`name`=:name,`phone`=:phone,`pwd`=:pwd,`age`=:age,`sex`=:sex,status=:status,last_time=:last_time';
$sql='INSERT INTO '.$this->table.' SET '.$fields;
$stmt=$this->pdo->prepare($sql);
//绑定数据
$stmt->bindParam('name',$data['name'],\PDO::PARAM_STR);
$stmt->bindParam('phone',$data['phone'],\PDO::PARAM_INT);
$stmt->bindParam('pwd',$data['pwd'],\PDO::PARAM_STR);
$stmt->bindParam('age',$data['age'],\PDO::PARAM_STR);
$stmt->bindParam('sex',$data['sex'],\PDO::PARAM_STR);
$stmt->bindParam('status',$data['status'],\PDO::PARAM_STR);
$stmt->bindParam('last_time',$data['last_time'],\PDO::PARAM_STR);
if($stmt->execute()){
/* if($stmt->rowCount()>0){
return '数据成功添加了'.$stmt->rowCount().'条';
}*/
return $stmt->rowCount()>0 ? '已经成功添加了'.$stmt->rowCount().'条数据。' : '数据没有添加成功,这是为什么呢';
}else{
return '数据库执行失败'.$stmt->errorInfo();
}
}
//读取所有数据
public function read($field='*',$where='',$limit='0,5')
{
$where=empty($where) ? ' ' : 'WHERE'.$where;
$field=empty($field) ? '*' : $field;
$limit=' LIMIT '.$limit;
$sql='SELECT '.$field.' FROM '.$this->table.$where.$limit;
$stmt=$this->pdo->prepare($sql);
$stmt->execute();
return $stmt->fetchAll(\PDO::FETCH_ASSOC);
}
public function update($data, $where)
{
//修改一下新增语句
$fields='`name`=:name,`phone`=:phone,`pwd`=:pwd,`age`=:age,`sex`=:sex,status=:status,last_time=:last_time';
$sql='UPDATE '.$this->table.' SET '.$fields.' WHERE '.$where;
$stmt=$this->pdo->prepare($sql);
//绑定数据
$stmt->bindParam('name',$data['name'],\PDO::PARAM_STR);
$stmt->bindParam('phone',$data['phone'],\PDO::PARAM_INT);
$stmt->bindParam('pwd',$data['pwd'],\PDO::PARAM_STR);
$stmt->bindParam('age',$data['age'],\PDO::PARAM_STR);
$stmt->bindParam('sex',$data['sex'],\PDO::PARAM_STR);
$stmt->bindParam('status',$data['status'],\PDO::PARAM_STR);
$stmt->bindParam('last_time',$data['last_time'],\PDO::PARAM_STR);
if($stmt->execute()){
if($stmt->rowCount()>0){
return '成功更新了'.$stmt->rowCount().'条数据';
}else{
return '更新数据失败';
}
}else{
return $stmt->errorInfo();
}
}
public function delete($where)
{
$sql='DELETE FROM '.$this->table.' WHERE '.$where;
$stmt=$this->pdo->prepare($sql);
if($stmt->execute()){
return $stmt->rowCount()>0 ? '已经成功删除了'.$stmt->rowCount().'条数据。' : '数据删除失败!';
}else{
return '错误信息为'.$stmt->errorInfo();
}
}
}
$dsn='mysql:host=127.0.0.1;dbname=www.tao.io';
$user='root';
$pass='root';
$db=new DB($dsn,$user,$pass);
//查询数据库
$db->read();
foreach($db->read() as $item){
echo '<pre>';
print_r($item);
echo '</pre>';
}
echo '<hr>';
//添加数据
//用来添加数据的数组
$data=[
'name'=>'小鬼',
'phone'=>135643124,
'pwd'=>2233421,
'age'=>12,
'sex'=>2,
'status'=>1,
'last_time'=>1561125595
];
echo $db->create($data);//输出一次查询
$db->read();
foreach($db->read() as $item){
echo '<pre>';
print_r($item);
echo '</pre>';
}
echo '<hr>';
//更新数据
//用来更新数据的数组
$data1=[
'name'=>'小新',
'phone'=>135643124,
'pwd'=>2233421,
'age'=>12,
'sex'=>2,
'status'=>1,
'last_time'=>1561125595
];
$upWhere=' uid=18';
echo $db->update($data1,$upWhere);//输出一次查询
$db->read();
foreach($db->read() as $item){
echo '<pre>';
print_r($item);
echo '</pre>';
}
echo '<hr>';
//删除数据
$deWhere=' uid = 31';
echo $db->delete($deWhere);//输出一次查询
$db->read();
foreach($db->read() as $item){
echo '<pre>';
print_r($item);
echo '</pre>';
}点击 "运行实例" 按钮查看在线实例
最后实现的效果

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