作业问题:

代码:

  1. <?php
  2. class Query
  3. {
  4. //创建类的唯一实例
  5. private static $db;
  6. protected $table;
  7. protected $field;
  8. protected $limit;
  9. private function __construct()
  10. {
  11. }
  12. static function connect($dsn,$username,$pwd)
  13. {
  14. if(is_null(static::$db)){
  15. static::$db = new PDO($dsn,$username,$pwd);
  16. }
  17. //返回query实例
  18. return new static();
  19. }
  20. public function table($table)
  21. {
  22. $this->table = $table;
  23. // echo $this->table;
  24. return $this;
  25. }
  26. public function field($field)
  27. {
  28. $this->field = $field;
  29. // echo $this->field;
  30. return $this;
  31. }
  32. public function limit($limit)
  33. {
  34. $this->limit = $limit;
  35. return $this;
  36. }
  37. public function getSql()
  38. {
  39. return 'SELECT '.$this->field.' FROM '.$this->table.' LIMIT '.$this->limit;
  40. // return sprintf('SELECT %s FROM %s LIMIT %d',$this->field,$this->table,$this->limit);
  41. }
  42. public function select(){
  43. return static::$db->query($this->getSql())->fetchALL(PDO::FETCH_ASSOC);
  44. }
  45. }
  46. class Db
  47. {
  48. static function __callStatic($method, $arygs)
  49. {
  50. $dsn = 'mysql:host=localhost;dbname=test_db';
  51. $username = 'test_db';
  52. $pwd = '123456';
  53. //获取委托实例
  54. $query = Query::connect($dsn,$username,$pwd);
  55. return call_user_func([$query,$method],...$arygs);
  56. }
  57. }
  58. //$res = Db::table('t_user')
  59. // ->field('name')
  60. // ->limit(1)
  61. // ->select();
  62. //
  63. //print_r($res);
  64. print_r(Db::filed('nihao'));

更多相关文章

  1. 《Android和PHP最佳实践》官方站
  2. android用户界面之按钮(Button)教程实例汇
  3. TabHost与RadioGroup结合完成的菜单【带效果图】5个Activity
  4. Android(安卓)UI开发第十七篇——Android(安卓)Fragment实例(Lis
  5. Android——Activity四种启动模式
  6. Android布局(序章)
  7. Android发送短信方法实例详解
  8. Android(安卓)读取资源文件实例详解
  9. android 蓝牙通讯

随机推荐

  1. 添加 C/C++ 代码
  2. android中系统自带样式
  3. Android(安卓)开发学习资料
  4. Android 实例子源代码文件下载地址380个
  5. Android基本控件常用属性及方法
  6. android facebook authorize 时禁止调用f
  7. Android之Android(安卓)Studio JNI
  8. android HTTPURLConnection解决不能访问H
  9. Android Firebase Dynamic Links 动态链
  10. 关于安卓 TextView 添加点击事件