批改状态:合格
老师批语:你能举一些与课堂不一样的案例吗?
1、自动加载
spl_autoload_register(callback)`: 通过回调自动加载外部文件
Loader.php
<?php
namespace _0802test;
class Loader
{
public static function autoLoader()
{
//`spl_autoload_register(callback)`: 通过回调自动加载外部文件
spl_autoload_register(function ($className){
$path=str_replace('\\','/',$className);
$path=__DIR__.'/'.$path.'.php';
// file_exists() 函数检查文件或目录是否存在。
if(file_exists($path)){
require $path;
}
});
}
}点击 "运行实例" 按钮查看在线实例
<?php
namespace _0802test;
## 1. 自动加载
//* `spl_autoload_register(callback)`: 通过回调自动加载外部文件
/*
require __DIR__.'/inc/Test1.php';
require __DIR__.'/inc/Test2.php';
echo \inc\Test1::get().'<br>';
echo \inc\Test2::get().'<br>';
echo '<hr>';
// ::class 获取一个类的完整名称
// 一个类的完整名称 = 命名空间 + 类名
echo \inc\Test1::class.'<br>';
echo \inc\Test2::class.'<br>';
echo '<hr>';
// 以 inc\Test3 为例
// 将类文件中的命名空间解析出来, 做为类文件的引入路径进行加载
echo \inc\Test3::class.'<br>';
//str_replace() 函数以其他字符替换字符串中的一些字符(区分大小写)。
$path=str_replace('\\','/',\inc\Test3::class);
$path=__DIR__.'/'.$path.'.php';
echo $path.'<br>';
require $path; // inc\Test3 这个类加载进来了
echo \inc\Test3::get();
*/
require 'Loader.php';
Loader::autoLoader();
echo \inc\Test3::get().'<br>';
echo \inc\Test1::get().'<br>';
echo \inc\Test2::get().'<br>';
echo \inc\Test4::get().'<br>';点击 "运行实例" 按钮查看在线实例
2、抽象类
<?php
namespace _0802test;
## 2. 抽象类
//* `abstract`: 定义抽象方法/抽象类
//* 类中只要有一个抽象方法, 该类就应该声明为抽象类
//* 抽象类只能被继承,不能实例化,并且抽象方法必须在子类全部实现
//* 实现抽象方法的子类方法可见性不能低于抽象方法原定义
//* 抽象方法是public, 子类方法只能是public
//* 抽象方法是protected, 子类方法只能是protected/public
// 创建出一个抽象类
// 1. 不能实例化, 不能用new
// 2. 类中的抽象方法, 必须在子类全部实现
abstract class Demo2
{
protected $name;
public function __construct($name='admin')
{
$this->name=$name;
}
public function getName()
{
return $this->name;
}
// 这个方法没有实现的过程, 变成了一个抽象方法
abstract public function setName($value);
}
// 子类来扩展/ 实现一个抽象类
class Sub extends Demo2
{
// 在子类中将抽象类中的一个抽象方法setName()具体实现一下
public function setName($value)
{
$this->name=$value;
}
}
$sub1=new Sub();
echo $sub1->getName().'<br>';
$sub2=new Sub('sub2');
echo $sub2->getName().'<br>';
// 调用子类中实现的抽象方法setName()
$sub2->setName('abc');
echo $sub2->getName();点击 "运行实例" 按钮查看在线实例
3、接口
<?php
namespace _0802test;
// 接口
// 1. 接口不能实例化
// 2. 接口中只允许出现抽象方法
// 3. 接口中的成员全部是公共的:public
// 4. 接口中允许有常量
// 5. 实现接口的类,必须将接口的抽象方法全部实现了
//* `interface`: 指定某个类必须实现的方法,但不需要定义方法的具体实现过程
//* 接口中仅允许出现: 抽象方法与类常量
//* 接口的方法可见性必须是: public
//* 接口的方法体必须是空的
//* 接口是类的代码模板, 可以像类一样有父子继承关系,例如父接口, 子接口
//* `implements`: 类实现接口的关键字, 读音: ['ɪmplɪmɛnts,应波罗曼次]
//* 如果仅是部分实现接口中的方法, 请用一个抽象类来实现它
// 创建了一个学校的接口
interface iSchool
{
//姓名
public function setName($name);
//职位
public function setPosition($position);
}
//学生
class Student implements iSchool
{
private $name;
protected $position;
//姓名
public function setName($name)
{
$this->name=$name;
}
//职位
public function setPosition($position='学生')
{
$this->position=$position;
}
// 在自定义类中, 可以扩展一个方法
public function getInfo()
{
return '姓名:'.$this->name.' 职位:'.$this->position.'<br>';
}
}
//教师
class Teacher implements iSchool
{
private $name;
protected $position;
//姓名
public function setName($name)
{
$this->name=$name;
}
//职位
public function setPosition($position='老师')
{
$this->position=$position;
}
public function getInfo()
{
return '姓名:'.$this->name.' 职位:'.$this->position.'<br>';
}
}
//Student 实例化
$stu1=new Student();
$stu1->setName('001');
$stu1->setPosition();
echo $stu1->getInfo();
$stu2=new Student();
$stu2->setName('002');
$stu2->setPosition('学生会会长');
echo $stu2->getInfo();
//Teacher 实例化
$tea1=new Teacher();
$tea1->setName('教师 001 ');
$tea1->setPosition();
echo $tea1->getInfo();
$tea2=new Teacher();
$tea2->setName('教师 002 ');
$tea2->setPosition('数学老师');
echo $tea2->getInfo();点击 "运行实例" 按钮查看在线实例
4、利用接口实现一个基本的数据库操作类(CURD)
<?php
namespace _0802test;
// 接口实战的小案例
// 定义一个接口, 实现数据库的常用操作: CURD
interface iCurd
{
// 增加数据
public function add($data);
// 读取数据
public function read($fileds,$where,$limit);
// 更新数据
public function update($data,$where);
// 删除数据
public function delete($where);
}
// 创建Db类,实现iCurd接口
class Db implements iCurd
{
// 数据库的连接对象
protected $pdo=null;
//数据表
protected $table;
// 构造方法: 连接数据库,并设置默认的数据表
public function __construct($dsn,$username,$password,$table)
{
$this->pdo =new \PDO($dsn,$username,$password);
$this->table=$table;
}
// 增加数据
public function add($data)
{
$keyArr=array_keys($data);
$set = '';
foreach ($keyArr as $value){
$set.=$value.'=:'.$value.',';
}
$set=rtrim($set,',');
$sql='insert into '.$this->table.' set '.$set;
$stmt=$this->pdo->prepare($sql);
$stmt->execute($data);
// 返回新增数量, 新增记录的id也返回
return[
'count'=>$stmt->rowCount(),
'id'=>$this->pdo->lastInsertId()
];
}
// 读取数据
public function read($fileds='*',$where='',$limit='')
{
// 设置查询条件
$where=empty($where)?'':' where '.$where;
$limit=empty($limit)?'':' limit '.$limit;
$sql='select '.$fileds.' from '.$this->table.$where.$limit;
$stmt=$this->pdo->prepare($sql);
$stmt->execute();
// 用二维数组返回所有数据
return $stmt->fetchAll(\PDO::FETCH_ASSOC);
}
// 更新数据
public function update($data,$where)
{
if($where==''){
return 0;
}
$keyArr=array_keys($data);
$set='';
foreach ( $keyArr as $value) {
$set.=$value.'=:'.$value.',';
}
$set=rtrim($set,',');
$sql='update '.$this->table.' set '.$set.' where '.$where;
$stmt=$this->pdo->prepare($sql);
$stmt->execute($data);
// 返回更新的数量
return $stmt->rowCount();
}
// 删除数据
public function delete($where)
{
if($where==''){
return 0;
}
$sql='delete from '.$this->table.' where '.$where;
$stmt=$this->pdo->prepare($sql);
$stmt->execute();
// 返回删除的数量
return $stmt->rowCount();
}
}
// 实例化Db
$dsn='mysql:host=localhost;dbname=listen0724';
$username='root';
$password='root';
$table='nav';
$db=new Db($dsn,$username,$password,$table);
//新增操作
$data1=[
'name'=>'download',
'alias'=>'下载中心',
'image'=>'nav.jpg'
];
//$res=$db->add($data1);
//echo '成功新增 '.$res['count'].' 条数据,新增的数据的ID是 '.$res['id'];
echo '<hr>';
//查询操作
$res1=$db->read();
foreach ($res1 as $re){
print_r($re);
echo '<br>';
}
echo '<hr>';
$res2=$db->read('nav_id,alias','nav_id>1','1,2');
foreach ($res2 as $re){
print_r($re);
echo '<br>';
}
echo '<hr>';
//更新操作
$data2=[
'name'=>'down',
'image'=>'image.jpg'
];
$where2='nav_id=11';
//$res3=$db->update($data2,$where2);
//echo $res3;
echo '<hr>';
//删除操作
$where3='nav_id>11';
//$res4=$db->delete($where3);
//echo $res4;点击 "运行实例" 按钮查看在线实例
5、利用接口实现一个基本的链式数据库操作类(CURD)
<?php
// 接口实战的小案例
// 定义一个接口, 实现数据库的常用操作: CURD
interface iCurd
{
// 增加数据
public function add();
// 读取数据
public function read();
// 更新数据
public function update();
// 删除数据
public function delete();
}
// 创建Db类,实现iCurd接口
class Db implements iCurd
{
// 数据库的连接对象
protected $pdo=null;
public $table;
public $field='*';
public $where;
public $limit;
public $data;
// 构造方法: 连接数据库,并设置默认的数据表
public function __construct($dsn,$username,$password)
{
$this->pdo =new \PDO($dsn,$username,$password);
}
// 设置表名
public function table($tableName)
{
$this->table=$tableName;
// 关键步骤: 返回一个当前类的实例
return $this;
}
// 设置数据
public function data($data='')
{
$this->data=empty($data)?'':$data;
return $this;
}
// 设置字段
public function field($fields='*')
{
$this->field=empty($fields)?'*':$fields;
return $this;
}
// 设置查询条件
public function where($where='')
{
$this->where=empty($where)?$where:' where '.$where;
return $this;
}
// 设置数量
public function limit($limit='')
{
$this->limit=empty($limit)?$limit:' limit '.$limit;
return $this;
}
// 增加数据
public function add()
{
$keyArr=array_keys($this->data);
$set = '';
foreach ($keyArr as $value){
$set.=$value.'=:'.$value.',';
}
$set=rtrim($set,',');
$sql='insert into '.$this->table.' set '.$set;
$stmt=$this->pdo->prepare($sql);
$stmt->execute($this->data);
// 返回新增数量, 新增记录的id也返回
return[
'count'=>$stmt->rowCount(),
'id'=>$this->pdo->lastInsertId()
];
}
// 读取数据
public function read()
{
$sql='select '.$this->field.' from '.$this->table.$this->where.$this->limit;
$stmt=$this->pdo->prepare($sql);
$stmt->execute();
// 用二维数组返回所有数据
return $stmt->fetchAll(\PDO::FETCH_ASSOC);
}
// 更新数据
public function update()
{
if($this->where==''){
return 0;
}
$keyArr=array_keys($this->data);
$set='';
foreach ( $keyArr as $value) {
$set.=$value.'=:'.$value.',';
}
$set=rtrim($set,',');
$sql='update '.$this->table.' set '.$set.$this->where;
$stmt=$this->pdo->prepare($sql);
$stmt->execute($this->data);
// 返回更新的数量
return $stmt->rowCount();
}
// 删除数据
public function delete()
{
if($this->where==''){
return 0;
}
$sql='delete from '.$this->table.$this->where;
$stmt=$this->pdo->prepare($sql);
$stmt->execute();
// 返回删除的数量
return $stmt->rowCount();
}
}
// 实例化Db
$dsn='mysql:host=localhost;dbname=listen0724';
$username='root';
$password='root';
$db=new Db($dsn,$username,$password);
//新增操作
$data1=[
'name'=>'download',
'alias'=>'下载中心',
'image'=>'nav.jpg'
];
//$res=$db->table('nav')
// ->data($data1)
// ->add();
//echo '成功新增 '.$res['count'].' 条数据,新增的数据的ID是 '.$res['id'];
echo '<hr>';
//查询操作
$res1=$db->table('nav')
->read();
foreach ($res1 as $re){
print_r($re);
echo '<br>';
}
echo '<hr>';
$res2=$db->table('nav')
->field('nav_id,alias')
->where('nav_id>1')
->limit('1,2')
->read();
foreach ($res2 as $re){
print_r($re);
echo '<br>';
}
echo '<hr>';
//更新操作
$data2=[
'name'=>'down1',
'image'=>'image1.jpg'
];
//$res3=$db->table('nav')
// ->data($data2)
// ->where('nav_id>14')
// ->update();
//echo $res3;
echo '<hr>';
//删除操作
//$res4=$db->table('nav')
// ->where('nav_id>15')
// ->delete();
//echo $res4;点击 "运行实例" 按钮查看在线实例
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号