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 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用