简体   繁体   English

我可以将变量传递给函数吗?

[英]Can I pass a variable into a function?

I'm trying to refactor some code but I'm kinda confused. 我正在尝试重构一些代码,但有点困惑。 I define my database connection like so: 我这样定义数据库连接:

try{
    global $conn;
    $conn = new PDO("mysql:host=$host",$root,$pw); [...]

Now I'd like a function for retrieving table rows but it needs $conn . 现在,我想要一个用于检索表行的函数,但它需要$conn Is there any way in which I can pass $conn into this function? 有什么方法可以将$conn传递给此函数? I tried to set it as a default value but that doesn't work: 我试图将其设置为默认值,但这不起作用:

function get($table,$conn=$conn,$limit=10){ [...]

I then tried the use keyword but I think it's only available for anonymous functions: 然后,我尝试了use关键字,但我认为它仅可用于匿名函数:

function get($table,$limit=10)use($conn){
    $query = $conn->query(" [...]

How do other people do this? 其他人如何做到这一点? Am I missing something obvious here? 我在这里错过明显的东西吗?

function get($table, $limit=10)

As you already wrote in your question, this function header is incomplete. 正如您在问题中所写的那样,此函数标头不完整。 The function itself can not do what it needs to do without having $conn . 没有$conn ,函数本身无法执行所需的操作。

As this is a function in the global namespace, the most straight forward thing could be to use a global variable: 由于这是全局名称空间中的函数,因此最直接的方法可能是使用全局变量:

function conn_get($table, $limit=10) {

    global $conn;

I also name-spaced the function to make the relation clear. 我还对函数命名,以使关系清楚。 The problem with this are two things: 问题是两件事:

  1. global functions are expensive to maintain 全局功能维护成本很高
  2. global variables are expensive to maintain 全局变量的维护成本很高

So what you normally do in that case is to wrap this into a class: 因此,在这种情况下,您通常要做的是将其包装到一个类中:

class Conn
{
    private $conn;

    public function __construct(PDO $conn) {

        $this->conn = $conn;
    }

    public function get($table, $limit=10) {

       $query = $this->conn->query("[...]");
       ...
    }
}

You then pass around a Conn object which can be used: 然后,您可以传递可使用的Conn对象:

$pdo  = new PDO("mysql:host=$host", $root, $pw);
$conn = new Conn($pdo);

And then: 接着:

$conn->get('ColorTable', 200);

The private variable takes over the role of the global variable with the benefit that every method inside the same object can access it. 私有变量具有全局变量的作用,其优点是同一对象内的每个方法都可以访问它。 So everything now is in it's own space and contrary to the global space, will not go into each others way that fast. 因此,现在的一切都在自己的空间中,与全球空间相反,不会那么快地进入彼此。 This is easy (easier) to change and maintain over time. 随着时间的推移,这很容易(轻松)进行更改和维护。

When you call the function ie: 当您调用函数时,即:

$table_rows = get($table, $conn);

You are passing local variables inside the function scope. 您正在函数范围内传递局部变量。

However you can't define a not-static variable as default: $conn=$conn will throw a fatal error. 但是,您不能将非静态变量定义为默认值: $conn=$conn将引发致命错误。

the most simple thing you can do is to create a function that will return you the $conn variable 您可以做的最简单的事情是创建一个函数,该函数将返回$conn变量

function conn (){
   $conn = NULL;
   ...some database connection setup etc...
   return $conn;
}

and call it to other functions that you need to use it 并将其调用到您需要使用的其他函数

function getDb(){
 conn()->query(" [...]");
}

the conn() function will be available to all your functions on your PHP script. conn()函数将对您的PHP脚本中的所有函数可用。

but if you plan to make a more complex web application I recommend you to use a PHP framework or make a PHP class and apply OOP principles that would handle the database connection for you. 但是,如果您打算制作一个更复杂的Web应用程序,我建议您使用PHP框架或制作一个PHP类,并应用可以为您处理数据库连接的OOP原则。

In PHP, use is the way to go for anonymous / lambda-functions but not for ordinary functions. 在PHP中, use匿名/ lambda函数是一种方法,但对于普通函数则不是。

If you have the database connection flying around in global scope, you can either pass it as a normal variable to your functions like so: 如果您的数据库连接在全局范围内运行,则可以将其作为普通变量传递给函数,如下所示:

function get(PDO $conn, $table,$limit=10) {
    $query = $conn->query(" [...]
}

Other than that (bad practice!) is to get the global $conn variable into the function like so: 除此之外(不好的做法!)是将全局$conn变量放入函数中,如下所示:

function get($table,$limit=10) {
    $query = $GLOBALS['conn']->query(" [...]
}

However, an object oriented approach is recommended! 但是,建议使用面向对象的方法! You might want to inject the Database Class via dependency injection into the classes, where you need it. 您可能需要通过依赖注入将数据库类注入到需要的类中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM