批改状态:合格
老师批语:
使用方法重载与call_user_func_array()模拟TP框架的链式查询
<?php
/**
* 跨类调用
*/
header('content-type:text/html;charset=utf-8');
require 'Query.php';
//数据库链式操作
$res = Db::table('player')
->fields('id,name,age,salary')
->where('salary > 0')
->select();
class Db{
public static function __callStatic($name, $arguments)
{
// TODO: Implement __callStatic() method.
// 调用回调函数,并把一个数组参数作为回调函数的参数。
//call_user_func_array([对象, 方法],[])
return call_user_func_array([(new Query1()),$name],$arguments);
}
}
//print_r($res);
$table = '<table border="1" width="60%" align="center" cellpadding="5"cellspacing="0">';
$table .= '<caption style="padding-bottom: 10px">球员信息表</caption>';
$table .= '<tr style="background: #43e6ff"><th>' .'ID'.'</th><th>'.'姓名'.'</th><th>'.'年龄'.'</th><th>'.'薪资'.'</th></tr>';
foreach($res as $p){
$table .='<tr align="center">';
$table .='<td>'.$p['id'].'</td>';
$table .='<td>'.$p['name'].'</td>';
$table .='<td>'.$p['age'].'</td>';
$table .='<td>'.$p['salary'].'</td>';
$table .='</tr>';
}
$table .= '</table>';
echo $table;点击 "运行实例" 按钮查看在线实例
后期静态绑定的原理与使用场景分析:
代码执行分两个阶段,前期:编译阶段,后期:运行阶段。
后期静态绑定就是执行代码时解析调用方法时的类,而不是实际运行的类,用于重写父类中的静态方法
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号