博主信息
博文 18
粉丝 0
评论 0
访问量 13528
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
方法重载与静态继承9月4日作业
吕小布的博客
原创
716人浏览过

实例

<?php

class Query
{
    private $sql=[];
    private $pdo=null;
    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 fields($fields)
    {
        $this->sql['fields']=$fields;
        return $this;
    }
    public function where($where)
    {
        $this->sql['where'] = $where;
        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);
    }
}

运行实例 »

点击 "运行实例" 按钮查看在线实例

实例

<?php
require 'Query1.php';
class Db
{
  public static function __callStatic($name,$arguments)
  {
    return call_user_func_array([(new Query()),$name],$arguments);
  }
}

$result = Db::table('user')->fields('id,name,email')->where('id > 1')->select();
//print_r($result);

// 用表格将查询结果格式化输出
$table = '<table border="1" cellpadding="5" cellspacing="0" width="60%" align="center">';
$table .= '<caption style="font-size: 1.5rem;margin:15px;">用户信息表</caption>';
$table .= '<tr bgcolor="#90ee90"><th>ID</th><th>姓名</th><th>邮箱</th></tr>';

foreach ($result as $user)
{
    $table .= '<tr align="center">';
    $table .= '<td>'.$user['id'].'</td>';
    $table .= '<td>'.$user['name'].'</td>';
    $table .= '<td>'.$user['email'].'</td>';
    $table .= '</tr>';
}

$table .= '</table>';
$num = '<p style="text-align: center"> 共计:  <span style="color:red">'.count($result).'</span>条记录</p>';
echo $table, $num;

运行实例 »

点击 "运行实例" 按钮查看在线实例

问答: 后期静态绑定的原理与使用场景分析

原理:子类调用父类同名方法并在子类进行了重写,通过static进行静态绑定,始终指向静态方法的调用者。

使用场景分析:当子类需要对父类同名方法重写并调用的情况。

批改状态:合格

老师批语:
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学