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 ssh登录页面的问题
  • ¥60 渗透一个指定银行app,拿到客户信息,需要什么级别
  • ¥50 关于在matlab上对曲柄摇杆机构上一点的运动学仿真
  • ¥15 jetson nano
  • ¥15 :app:debugCompileClasspath'.
  • ¥15 windows c++内嵌qt出现数据转换问题。
  • ¥20 公众号如何实现点击超链接后自动发送文字
  • ¥15 用php隐藏类名和增加类名
  • ¥15 算法设计与分析课程的提问
  • ¥15 用MATLAB汇总拟合图