替代在PHP中使用全局变量

时间:2017-09-04 00:19:16

标签: php

我开发了一个运行查询的函数,然后在另一个文件的逻辑中使用结果变量。我使用全局变量来实现这一目标。我已经读过要避免的全局变量,是否有另一种方法来编写这个函数以避免使用全局变量。

文件1(功能文件)

<?php 

    function usertype() 
    {

        include "../classes/sessionstart.php";
        include "../config/dbconnect.php";

        $user_id = $_SESSION['user_id'];
        $select = $con->prepare("SELECT user_usertype, user_gender FROM tbl_user WHERE user_id = $user_id");
        $select->setFetchMode(PDO::FETCH_ASSOC);
        $select->execute();
        while($data=$select->fetch()){

            $GLOBALS['gender'] = $data['user_gender'];  
            $GLOBALS['usertype']  = $data['user_usertype'];
        }
    }

?>

文件2(使用功能文件的文件)

<?php
    usertype();
?>
<div>
    <select class="searchpropertyinputs" name="user_usertype" id="user_usertype">
        <?php if ($gender == "Male" && $usertype != "Marriage Bureau") { ?> <option value="Bride">Bride</option> <?php } ?>
        <?php if ($gender == "Female" && $usertype != "Marriage Bureau") { ?> <option value="Groom">Groom</option> <?php } ?>
        <?php if ($usertype == "Marriage Bureau") { ?>
            <option value="" hidden>Bride or Groom</option>
            <option value="Bride">Bride</option>
            <option value="Groom">Groom</option>
        <?php } ?>  
    </select>
</div>

1 个答案:

答案 0 :(得分:2)

您应该从函数中返回值:

更新了代码,修复了准备好的查询,并为返回列设置了别名。

<?php

    function usertype() 
    {
        include_once "../classes/sessionstart.php";
        include_once "../config/dbconnect.php";

        $select = $con->prepare("
            SELECT user_usertype as `type`,
                   user_gender   as `gender`
            FROM tbl_user
            WHERE user_id = :user_id LIMIT 1
        ");
        $select->bindValue(':user_id', (int) $_SESSION['user_id'], PDO::PARAM_INT);
        $select->execute();

        return $select->fetch(PDO::FETCH_ASSOC);
    }

?>

<?php
    $usertype = usertype();
?>
<div>
    <select class="searchpropertyinputs" name="user_usertype" id="user_usertype">
        <?php if ($usertype['gender'] == "Male" && $usertype['type'] != "Marriage Bureau") { ?> <option value="Bride">Bride</option> <?php } ?>
        <?php if ($usertype['gender'] == "Female" && $usertype['type'] != "Marriage Bureau") { ?> <option value="Groom">Groom</option> <?php } ?>
        <?php if ($usertype['type'] == "Marriage Bureau") { ?>
            <option value="" hidden>Bride or Groom</option>
            <option value="Bride">Bride</option>
            <option value="Groom">Groom</option>
        <?php } ?>  
    </select>
</div>

您可能还希望移出includes进行连接,会话启动,否则将来会遇到问题。因此,这很可能会引导您将功能分组到用户类中,例如:

<?php

include_once "../classes/sessionstart.php";
include_once "../config/dbconnect.php";

class User {

    public function __construct(PDO $con)
    {
        $this->con = $con;
    }

    public function type($user_id = 0) 
    {
        $select = $this->con->prepare("
            SELECT user_usertype as `type`,
                   user_gender   as `gender`
            FROM tbl_user
            WHERE user_id = :user_id LIMIT 1
        ");
        $select->bindValue(':user_id', (int) $user_id, PDO::PARAM_INT);
        $select->execute();

        return $select->fetch(PDO::FETCH_ASSOC);
    }

    //...
}

$user = new User($con);

$usertype = $user->type($_SESSION['user_id']);
?>