批改状态:合格
老师批语:
今天是第十八天上课,朱老师讲了static方法的重载和后期静态绑定用法。
重点部分:1. 静态方法重载:__callStatic();普通方法重载:__call()。
2. call_user_func_array():用户自定义回调方法。
普通用法:call_user_func_array(function($a,$b){$a+$b},[$a,$b]);
回调对象方法:call_user_func_array([ 对象 , ' 方法 '], [$arguments]);
会滴啊拍静态方法:call_user_func_array([ ' 类名 ', ' 方法 '], [$arguments]);
3. 运行阶段才确定方法的调用者的技术: 后期[运行阶段]静态绑定, 延迟静态绑定;
用了static的静态方法始终只想调用者。
1. 使用方法重载与call_user_func_array()模拟TP框架的链式查询
<?php
require 'Requery.php';
class Db
{
//静态方法重载
public static function __callStatic($name,$arguments)
{
//利用回调函数调用Requery中的方法
return call_user_func_array([(new Requery),$name],$arguments);
}
}
$result = Db::table('staff')//下面是对象访问符,因此这里需要返回一个类实例
->fields('id,name,salary')
->where('salary>=8000')
->select();
//echo '<pre>';
//print_r($result);
//将结果集放到table中显示
$table = '<table border="1" cellpadding="5" cellspacing="0" width="60%" align="center">';
$table .= '<caption style="font-size: 1.5rem;">工资表</caption>';
$table .= '<tr bgcolor="cyan"><th>ID</th><th>姓名</th><th>工资</th></tr>';
foreach ($result as $staff) {
$table .= '<tr align="center">';
$table .= '<td>'.$staff['id'].'</td>';
$table .= '<td>'.$staff['name'].'</td>';
$table .= '<td>'.$staff['salary'].'</td>';
$table .= '</tr>';
}
$table .= '</table>';
$num = '<p style="text-align: center"> 共计: <span style="color:red">'.count($result).'</span> 条记录</p>';
echo $table, $num;点击 "运行实例" 按钮查看在线实例
Requery.php
<?php
header('Content-type:text/html;charset=utf-8');
class Requery
{
//创建变量来存放sql语句
private $sql = [];
//创建数据库连接对象
private $pdo = null;
//构造方法实例化PDO
public function __construct()
{
$this->pdo = new PDO('mysql:host=127.0.0.1;dbname=php','root','root');
}
public function table($name)
{
$this->sql['table'] = $name;
return $this;//返回的是整个类实例
}
public function fields($fields)
{
$this->sql['fields'] = $fields;
return $this;
}
public function where($where)
{
$this->sql['where'] = $where;
return $this;
}
public function select()
{
//将变量拼起来组成sql语句并执行
$sql = "SELECT {$this->sql['fields']} FROM {$this->sql['table']} WHERE {$this->sql['where']}";
$stmt = $this->pdo->prepare($sql);
$stmt->execute();
return $stmt->fetchAll(PDO::FETCH_ASSOC);//返回结果集
}
}点击 "运行实例" 按钮查看在线实例
2. 后期静态绑定的原理与使用场景分析
<?php
class Db1
{
public static $pdo = null;
//连接数据库
public static function connect()
{
self::$pdo = new PDO('mysql:host=127.0.0.1;dbname=php','root','root');
}
public static function query()
{
$stmt = self::$pdo->prepare("SELECT `name`,`salary` FROM `staff` LIMIT 5");
$stmt->execute();
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
public static function select()
{
self::connect();
// return self::query();//这里会执行Db1的query()方法
return static::query();//这里才会执行调用类的query()方法
}
}
class Db2 extends Db1
{
//重写query()方法
public static function query()
{
$stmt = self::$pdo->prepare("SELECT `name` ,`email` FROM `user` LIMIT 5");
$stmt->execute();
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
}
echo '<pre>';
//print_r(Db1::select());
print_r(Db2::select());点击 "运行实例" 按钮查看在线实例
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号