Correcting teacher:天蓬老师
Correction status:qualified
Teacher's comments:为什么要把一个功能分到多个类, 或者trait中, 原因只有一个, 就是为了好维护, 这样就可以把不同的功能模板交给不同的人处理
接口可以突破php类的继承限制, 允许多继承, 形成了多层级的接口
必须实现接口中的抽象方法
<?php
//接口
interface iSchool
{
//接口常量
const school="清华大学";
}
interface iUser extends iSchool
{
//接口常量
const name ="张三";
const age = 23;
}
//接口实现多继承
interface Infor extends iUser, iSchool
{
//接口方法
public function print();
}
//实现类
class Inf implements iSchool, Infor, iUser
{
public function print(){
return "姓名:". iUser::name."<br>年龄:". iUser::age."<br>就读学校:". iSchool::school;
}
}
//客户端实现
echo (new Inf)->print();
echo "<br>";
//实现类
class Inf1 implements Infor
{
public function print(){
return "姓名:". iUser::name."<br>年龄:".iUser::age."<br>就读学校:".iSchool::school;
}
}
//客户端实现
echo (new Inf1)->print();
?>
<?php
// 用抽象类来部分实现一个接口
interface iDb
{
// 接口方法
public static function insert($db, $data);
public static function select($db, $options=[]);
public static function update($db,$options);
public static function delete($db, $where);
// 连接数据库
public static function connect($dsn, $usrname, $pwd);
}
// 用一个抽象类只实现接口的连接方法: connect()
abstract class aDb implements iDb
{
protected static $db = null;
public static function connect($dsn, $username, $pwd)
{
self::$db = new PDO($dsn,$username,$pwd);
return self::$db;
}
}
// 工作类: 通用查询
class DB extends aDb
{
public static function insert($db, $data)
{
}
public static function select($db, $options=[])
{
$sql = 'select * from admins limit 5';
return $db->query($sql);
}
public static function update($db,$options)
{
}
public static function delete($db, $where)
{
}
}
// 客户端
$config = [
'type' => $type ?? 'mysql',
'host' => $hsot ?? 'localhost',
'dbname' => $dbname ?? 'xxyl',
'charset' => $charset ?? 'utf8',
'port' => $port ?? '3306',
'username' => $username ?? 'root',
'password' => $pwd ?? 'root',
];
// 创建PDO数据源
$dsn = sprintf('%s:host=%s;dbname=%s;',$config['type'],$config['host'],$config['dbname']);
// 使用自定义用户密码
$username = $config['username'];
$pawd = 'root';
// 连接数据库: 实际上调用的是抽象类中的connect()方法完成,返回连接对象$db
$db = DB::connect($dsn, $username, $pawd);
// 查询操作
$users = DB::select($db);
// 遍历结果集
foreach ($users as list('id'=>$id, 'name'=>$name,'phone'=>$phone)) {
printf('%s => %s <br>', $id, $name,$phone);
}
?>
<?php
// 接口来实现多态
interface iDb
{
// 接口方法
public static function insert($db, $data);
public static function select($db, $options=[]);
public static function update($db,$options);
public static function delete($db, $where);
// 连接数据库
public static function connect($dsn, $usrname, $pwd);
}
// 用一个抽象类只实现接口的连接方法: connect()
class PDO_Db implements iDb
{
protected static $db = null;
public static function connect($dsn, $username, $pwd)
{
self::$db = new PDO($dsn,$username,$pwd);
return self::$db;
}
public static function insert($db, $data)
{
}
public static function select($db, $options=[])
{
}
public static function update($db,$options)
{
}
public static function delete($db, $where)
{
}
}
class SQL_Db implements iDb
{
protected static $db = null;
public static function connect($dsn, $username, $pwd)
{
self::$db = new PDO($dsn,$username,$pwd);
return self::$db;
}
public static function insert($db, $data)
{
}
public static function select($db, $options=[])
{
}
public static function update($db,$options)
{
}
public static function delete($db, $where)
{
}
}
// 工作类: 通用查询
class DB
{
public static function insert($db, $data)
{
}
public static function select($db, $options=[])
{
$sql = 'select * from admins limit 5';
return $db->query($sql);
}
public static function update($db,$options)
{
}
public static function delete($db, $where)
{
}
}
// 客户端
$config = [
'type' => $type ?? 'mysql',
'host' => $hsot ?? 'localhost',
'dbname' => $dbname ?? 'xxyl',
'charset' => $charset ?? 'utf8',
'port' => $port ?? '3306',
'username' => $username ?? 'root',
'password' => $pwd ?? 'root',
];
// 创建PDO数据源
$dsn = sprintf('%s:host=%s;dbname=%s;',$config['type'],$config['host'],$config['dbname']);
// 使用自定义用户密码
$username = $config['username'];
$pawd = 'root';
$db = PDO_Db::connect($dsn, $username, $pawd);
$db = SQL_Db::connect($dsn, $username, $pawd);
// 查询操作
$users = DB::select($db);
// 遍历结果集
foreach ($users as list('id'=>$id, 'name'=>$name,'phone'=>$phone)) {
printf('%s => %s <br>', $id, $name,$phone);
}
?>
<?php
trait User{
// 常规
protected $name = '李明';
public function getName()
{
return $this->name;
}
// 静态
public static $sex = '男';
public static function getSex()
{
return self::$sex;
}
// 抽象
// 抽象静态属性
public static $School;
// 抽象静态方法
abstract public static function getSchool();
}
class Infor {
public static function getSchool()
{
return self::$School;
}
use User;
}
$inf = new Infor();
Infor::$School="清华大学";
echo $inf->getName() .'-->'.$inf->getSex().'-->'.$inf->getSchool();
本节课我们学习了接口与trait的基本功能,学习到了php通过接口实现多继承,以及多态的概念,对trait知识有了初步的了解。通过以后的实践来深层次了理解接口与trait的基本功能。