批改状态:未批改
老师批语:
通过今天的学习,我学会了类的方法重载,并且可以模拟数据库链式链接,还明白了后期绑定的基础知识点,以下是我的编程代码:
1,使用方法重载与call_user_func_array()模拟TP框架的链式查询操作
a:query类
<meta charset="UTF-8">
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2018/9/5
* Time: 12:52
*/
class query
{
private $sql=[];
private $pdo=null;
//创建构造方法,在初始化的时候,即可以创建对象
public function __construct()
{
$this->pdo=new PDO('mysql:host=127.0.0.1;dbname=test','root','root');
}
//创建table方法
public function table($table)
{
$this->sql['table']=$table;
return $this;
}
//创建fileds字段,主要返回查询到的字段
public function fields($fields)
{
$this->sql['fields']=$fields;
return $this;
}
//创建where条件
public function where($where)
{
$this->sql['where']=$where;
return $this;
}
//创建参数
public function param($param)
{
$this->sql['param']=$param;
return $this;
}
//创建执行条件
public function select()
{
$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);
}
public function insert()
{
$sql="INSERT into {$this->sql['table']} ({$this->sql['fields']}) VALUES ({$this->sql['param']})";
//创建预编译对象
$stmt=$this->pdo->prepare($sql);
$stmt->execute();
return $stmt->rowCount();
}
}点击 "运行实例" 按钮查看在线实例
b:执行查询:
<meta charset="UTF-8">
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2018/9/5
* Time: 10:07
*/
require 'query.php';
class lianxi3{
//静态方法重构,即使在类外调用不存在的方法,也不会报错
public static function __callStatic($name, $arguments)
{
// TODO: Implement __callStatic() method.
//第一个是类或对象,第二个是方法,第三个是数组形式参数
return call_user_func_array([(new query()),$name],$arguments);
}
}
$result=lianxi3::table('test')
->fields('name,age')
->where('age>0')
->select();
//echo '<pre>',var_dump($result);
$table ='<table border="1" cellpadding="5" cellspacing="0" width="60%" align="center">';
//表头
$table .='<caption>信息显示表</caption>';
$table .='<tr><th>姓名</th><th>年龄</th></tr>';
for($i=0;$i<count($result);$i++)
{
$table .='<tr>';
$table .= '<td>'.$result[$i]['name'].'</td>';
$table .= '<td>'.$result[$i]['age'].'</td>';
$table .='</tr>';
}
$table .='</table>';
$num = '<p style="text-align: center"> 共计: <span style="color:red">'.count($result).'</span> 条记录</p>';
echo $table,$num;
include 'lianxi4.php';点击 "运行实例" 按钮查看在线实例
c:执行增加
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2018/9/5
* Time: 16:07
*/
include 'query.php';
class lianxi5{
//静态方法重构,即使在类外调用不存在的方法,也不会报错
public static $age='1';
public static $name='2';
public static function __callStatic($name, $arguments)
{
// TODO: Implement __callStatic() method.
//第一个是类或对象,第二个是方法,第三个是数组形式参数
return call_user_func_array([(new query()),$name],$arguments);
}
public static function show()
{
$age= $_POST['age'];
$name= $_POST['name'];
return $name.','.$age;
}
}
$result=lianxi5::table('test')
->fields('name,age')
->param(lianxi5::$name,lianxi5::$age)
->insert();
if($result>0)
{
echo '添加成功!';
}else
{
echo '添加失败';
}点击 "运行实例" 按钮查看在线实例
总结:
后期绑定使用static::调用
call_user_func_array([(new query()),$name],$arguments);
第一个参数为类和对象,第二个参数方法,第三个为方法参数
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号