PHP:使其他函数访问数据库连接函数内的$ conn变量 [英] PHP : Make other functions access the $conn variable inside my database connection function

查看:77
本文介绍了PHP:使其他函数访问数据库连接函数内的$ conn变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让其他函数访问数据库连接函数内的$ conn变量

所以在这里,我绝对想尽一切办法.我知道我想做的不是OOP也不是100%最佳实践.它不是一个实时网站,我只是在XAMPP上学习一些基本的PHP概念.

So here I am absolutely desperate trying to make something work. I know what im trying to do is not OOP neither 100% best practice. It is not for a live website, I am just learning some basic PHP concepts on XAMPP.

我想做的是使数据库连接函数中的$ conn变量可用于需要它的所有其他函数.我正在考虑将其作为参数传递,但是如何做到这一点?我更喜欢不使用PHP的"global"或$ GLOBALS.我现在的工作方法是使用过程方法在mysqli中使用.

What I am trying to do is to make the $conn variable inside my database connection function accessible to all other functions that need it. I am thinking of passing it as a parameter, but how can this be done? I prefer not using PHP's "global" or $GLOBALS. My method of working right now is with mysqli using procedural methods.

例如,我有这样的东西:

For example I have something like this:

function db () {
$conn = mysqli_connect ("localhost", "root", "", "database");
}

function someFunction () {
$result = mysqli_query ($conn, "SELECT * FROM examples)
}

我从来没有找到解决问题的答案……最近我熟悉的大多数解决方案都是基于OOP的,或者使用了一些有问题的方法.

I never found the answer to my issue...most solutions which I recently got familiar with are OOP based or use somewhat questionable methods.

-------------------------------------------- -------------------------------------------------- --------------------------------------

解决方案A-我宁愿避免将我的连接放在包装器中,也不要使用全局变量:

global $conn = mysqli_connect ("localhost", "root", "", "database");
global $conn;

function someFunction () {
global $conn;
$result = mysqli_query ($conn, "SELECT * FROM examples)
}

解决方案B-我还没有准备好进行面向对象的设计,但是我知道这是可行的.关键是我想学习一些不同的东西:

class Database
{
    private static $conn;

    public static function getObject()
    {
        if (!self::$conn)
            self::$conn = new mysqli("localhost", "root", "", "database");

        return self::$conn;
    }
}

function someFunction () {
$result = mysqli_query (Database::$conn, "SELECT * FROM examples)
}

解决方案C-根本不使用函数...只是保持展开状态,从长远来看,我认为这不太实用:

$conn = mysqli_connect ("localhost", "root", "", "database");

$result = mysqli_query ($conn, "SELECT * FROM examples)

-------------------------------------------- -------------------------------------------------- --------------------------------------

我要尝试的解决方案:

function db () {
$conn = mysqli_connect ("localhost", "root", "", "database");
return $conn;
}

function someFunction () {
$conn = db ();
$result = mysqli_query ($conn, "SELECT * FROM examples)
}

或类似这样的事情,我只是将连接作为参数或其他内容(此刻为伪代码)传递给我们

function db () {
$conn = mysqli_connect ("localhost", "root", "", "database");
}

function someFunction ($conn) {
$result = mysqli_query ($conn, "SELECT * FROM examples)
}

-------------------------------------------- -------------------------------------------------- --------------------------------------

因此,我如何实现类似于后两者的效果,但实际上是可行的.这个概念可能吗?

推荐答案

您所需的解决方案:这应该可行,并且您只能建立一个连接.

Your Desired Solution: This should work, and you'll only make one connection.

function db () {
    static $conn;
    if ($conn===NULL){ 
        $conn = mysqli_connect ("localhost", "root", "", "database");
    }
    return $conn;
}

function someFunction () {
    $conn = db();
    $result = mysqli_query ($conn, "SELECT * FROM examples);
}

如果使用function someFunction($conn),这将使您的代码更加混乱,因为实际上您无法从任何地方通用访问$conn.

If you used the function someFunction($conn), that would make your code much messier, since you wouldn't actually have universal access to $conn from anywhere.

您应该使用解决方案B IMO.这样,您可以简单地访问它Database::$conn,这将在整个脚本中保持一致.您可以应该具有一个initialize函数(可以根据需要使用其他名称),该函数将初始化Database::$conn,然后可以使用该函数来初始化Database类中的其他内容.以后,如果需要的话.

You should go with Solution B IMO. That way, you can have simple access to it Database::$conn which will be consistent throughout your script. You could should have an initialize function (you could use a different name if you want) that will initialize Database::$conn, and you can then use that to initialize other things on the Database class later, if desired.

解决方案A 很糟糕.我做了很长时间(global东西化),这是一个可怕的想法.我不应该那样做.但是我做到了.我学到了.它只是使代码变得越来越草率.

Solution A is terrible. I did that for a long time (globalizing things), and it was a horrible idea. I should have never done that. But I did. And I learned. It just made code get progressively sloppier and sloppier.

解决方案B:如果要能够通过Database::$conn从任何地方访问它,则Database::$conn应该为public.如果是私人的,那么您总是需要致电Database::getObject();

Solution B: Database::$conn should be public if you want to be able to access it by Database::$conn from anywhere. If it's private, then you would always need to call Database::getObject();

解决方案C:您是对的.那将是非常不切实际的.

Solution C: You're right. That would be very impractical.

解决方案B重写:

class Database
{
    /** TRUE if static variables have been initialized. FALSE otherwise
    */
    private static $init = FALSE;
    /** The mysqli connection object
    */
    public static $conn;
    /** initializes the static class variables. Only runs initialization once.
    * does not return anything.
    */
    public static function initialize()
    {
        if (self::$init===TRUE)return;
        self::$init = TRUE;
        self::$conn = new mysqli("localhost", "root", "", "database");
    }
}

然后...在使用前至少调用一次Database::initialize().

Then... call Database::initialize() at least once before it gets used.

<?php
Database::initialize();
$result = mysqli_query (Database::$conn, "SELECT * FROM examples);
?>

编辑

  • 您也可以在该类的声明后立即在该PHP文件中调用Database::initialize().然后进行初始化.
  • 与直接访问$conn属性相比,我现在更喜欢Database::getDb()之类的东西.然后可以从getDb()函数调用initialize.基本上像所需的解决方案一样,但在一个类中.确实没有必要上课,但是如果您像我一样喜欢上课,那就很好了.
  • You can also call Database::initialize() immediately after the declaration of the class, in that PHP file. Then initializing is handled.
  • I'm now far more fond of something like Database::getDb() than accessing the $conn property directly. Then initialize can be called from the getDb() function. Basically like the Desired Solution but inside a class. The class really isn't necessary, but it can be nice if you like classes, like I do.

这篇关于PHP:使其他函数访问数据库连接函数内的$ conn变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆