[PHP] 模拟Zend Framework、ThinkPHP中的连贯操作 →→→→→进入此内容的聊天室

来自 , 2019-12-31, 写在 PHP, 查看 137 次.
URL http://www.code666.cn/view/bd852825
  1. <?php
  2. class Test
  3. {
  4.     protected $options = array();
  5.  
  6.     //这里就是了, 通过判断调用的函数名, 如果存在, 那么设置参数, 返回自己
  7.     public function __call($func, $args)
  8.     {
  9.         if (in_array($func, array(
  10.             'form',
  11.             'field',
  12.             'join',
  13.             'order',
  14.             'where',
  15.             'limit',
  16.             '更多....'
  17.         )))
  18.         {
  19.             $this->options[$func] = $args;
  20.             return $this; //这里返回了本对象
  21.         }
  22.     }
  23. }
  24. $test = new Test();
  25. $test->form('test'); // 这样调用就相当于设置 $test->options['form'] = 'test';
  26. //在ThinkPHP中这种连贯操作都是以find或者findAll结尾的.
  27. //所以前面这些方法的调用只是在设置查询的参数而已
  28. //在find或者findAll方法中, 是根据$this->options参数的不同执行不同的SQL
  29. //比如这样
  30. public function find() {
  31. $sql = \\"SELECT {$this->options['field']} FROM {$this->options['form']}\\";
  32. $sql .= isset($this->options['where']) ? \\" WHERE {$this->options['where']}\\" : '';
  33. // More  
  34. echo $sql;
  35. }
  36. //该片段来自于http://yuncode.net
  37.  

回复 "模拟Zend Framework、ThinkPHP中的连贯操作"

这儿你可以回复上面这条便签

captcha