批改状态:未批改
老师批语:
使用方法重载与call_user_func_array()模拟TP框架的链式查询
<?php
header("Content-Type:text/html;charset=utf-8");
class Query
{
//SELCET id name QQ FROM table WHERE slary>4000
private $pdo=null;
private $sql=[];
public function __construct()
{
$this->pdo = new PDO('mysql:host=127.0.0.1;dbname=php','root','root');
}
public function table($table)
{
$this->sql['table'] = $table;
return $this;
}
public function field($field)
{
$this->sql['field'] = $field;
return $this;
}
public function limit($limit)
{
$this->sql['limit'] = $limit;
return $this;
}
public function select()
{
$sql = "SELECT {$this->sql['field']} FROM {$this->sql['table']} WHERE {$this->sql['limit']}";
$stmt = $this->pdo->prepare($sql);
$stmt->execute();
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
}
?><?php
header("Content-Type:text/html;charset=utf-8");
require('ex3.php');
class Db
{
public static function __callStatic($name,$arguments)
{
return call_user_func_array([new Query(),$name],$arguments);
}
}
$result = Db::table('admin')
->field('id,name,QQ')
->limit('salary > 4000')
->select();
print_r($result);
$table = '<table border="1" cellpadding="5" cellspacing="0" width = "60%" alien = "center">';
$table .= '<caption>员工信息表</caption>';
$table .= '<tr><th>ID</th> <th>姓名</th> <th>QQ</th>';
foreach($result as $inf)
{
$table .='<tr>';
$table .='<td>'.$inf['id'].'</td>';
$table .='<td>'.$inf['name'].'</td>';
$table .='<td>'.$inf['QQ'].'</td>';
$table .='</tr>';
}
$table .='</table>';
echo $table;
?>后期静态绑定的原理与使用场景分析:
当子类和父类有相同的方法名字时,如果想调用父类方法,就必须用到static::function name。反之用self::function即可。
static始终指向静态。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号