dtsi9484 2017-05-26 14:01
浏览 37
已采纳

在php中用$ GLOBALS声明数据库表名

Problem

I'm using $GLOBALS[] variable to define names of tables from DB in my php files to make it simple changing names of tables and for general mobility/comfort. BUT..

I've heard that using $GLOBALS[] is SOMEHOW bad or something..


So, it's happening like this:

Tables in Database

Users
Orders
Products

Actually there are 10-20 tables...

global_vars.php

<?php

    $GLOBALS['t_users'] = 'Users';
    $GLOBALS['t_users'] = 'Orders';
    $GLOBALS['t_users'] = 'Products';

    //...
    //etc.

?>

Now, when I need to access DB from different pages of website and with different purposes I do it like so:

function GetUsers(){
    $sql = "SELECT * FROM $GLOBALS[t_users]";
    // ...execute
}
function Get_OneUser($id){
    $param['id'=>$id];
    $sql = "SELECT * FROM $GLOBALS[t_users] WHERE id=:id";
    // ...etc
}
function Get_Orders(){
    $sql = "SELECT * FROM $GLOBALS[t_orders]";
    // ...etc
}
function Get_OrdersB(){
    $sql = "SELECT * FROM $GLOBALS[t_orders] WHERE id=:id";
    // ...etc
}
function Get_Products(){
    $sql = "SELECT * FROM $GLOBALS[t_products]";
    // ...etc
}
//   -- AND SO ON, AND SO ON........

Imagine, one day I will need to rename tables/change database. Then (with this structure) I can only change one line in global_vars.php.. It will be perfect!


General Question

What could be a better way to make it?

Why this is/isn't good/bad? Help me figure it out! Thanks!

  • 写回答

2条回答 默认 最新

  • duanchao1002 2017-05-26 14:19
    关注

    TL;DR: yes, but it depends what you want to improve.

    The first issue is the global state. In PHP the $GLOBALS belongs to the group constructs, that are called "superglobals" and the issues they create are basically the same as with global variables.

    The general idea of keeping the table names in a separate configuration file is a good one, but would suggest to structure it all a bit differently:

    • the SQL should go in the data mappers
    • initialization of data mappers should be done via factory or by using DI Container (are proper one, like Auryn or Symfony DI, and not some shitty service locator like Pimple)
    • pass the configuration to the mapper, through constructor as a parameter

    Update

    Data mappers are classes, that are responsible for interacting with persistence (usually database). The implementation details may vary, but the way I implement them, the usage ends up looking kinda like this:

    $book = new Book;
    $book->setId(51);
    
    $mapper = new BookMapper($pdo, $config);
    $mapper->fetch($book);
    
    if ($book->getReleaseDate() < TWO_MONTHS_AGO) {
        $book->setDiscountPercents(30);
    }
    
    $mapper->store($book);
    

    For more "practical" code, you can try looking at this class.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失
  • ¥15 springboot+vue 集成keycloak sso到阿里云
  • ¥15 win7系统进入桌面过一秒后突然黑屏
  • ¥30 backtrader对于期货交易的现金和资产计算的问题
  • ¥15 求C# .net4.8小报表工具
  • ¥15 安装虚拟机时出现问题
  • ¥15 Selenium+docker Chrome不能运行
  • ¥15 mac电脑,安装charles后无法正常抓包
  • ¥18 visio打开文件一直显示文件未找到