首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >PHP面向对象之魔术方法__call应用场景(基础)

PHP面向对象之魔术方法__call应用场景(基础)

作者头像
友儿
发布2022-09-09 17:09:50
发布2022-09-09 17:09:50
2570
举报
文章被收录于专栏:友儿友儿
  • 工作大概2年多了,一提魔术常量都知道。但是在很多情况下,我们只知道某个函数的用法以及传参,但是不知道其应用场景。
  • 今天写一个小的应用场景,不多废话,直接上代码。
代码语言:javascript
复制
<?php
/**
 * Created by ZhengNiu.
 * User: 77103
 * Date: 2019/4/4
 * Time: 15:19
 */

namespace app\helper;

class db
{
  private $sql = [
      'table' => '',
      'field' => '*',
      'where' => '',
      'order' => '',
      'limit' => ''
  ];
  protected $where = '';
  protected $order = '';
  protected $limit = '';

  public function __call($name, $arguments)
  {
      if (array_key_exists($name, $this->sql)) {
          $this->sql[$name] = $arguments[0];
      }
      return $this;
  }

  public function select()
  {
      if (!empty($this->sql['where'])) {
          $this->where = ' where ' . $this->sql['where'];
      }
      if (!empty($this->sql['order'])) {
          $this->order = ' order by ' . $this->sql['order'];
      }
      if (!empty($this->sql['limit'])) {
          $this->limit = ' limit ' . $this->sql['limit'];
      }
      $sql = 'select ' . $this->sql['field'] . ' from ' . $this->sql['table'] . $this->where . $this->order . $this->limit;
      echo $sql;
  }
}
// 调用
$db = new db();
$db->table('user')->field('id,username,email,age')->where("email like '%qq.com%'")->order('id desc')->limit('1,10')->select();
Logic::vd($db);
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档