Passing database connection by reference in PHP

后端 未结 7 2188
清歌不尽
清歌不尽 2020-12-31 09:05

The question is if a database connection should be passed in by reference or by value?

For me I\'m specifically questioning a PHP to MySQL connection, but I think it

7条回答
  •  無奈伤痛
    2020-12-31 09:52

    Call-time pass-by-reference is being depreciated,so I wouldn't use the method first described. Also, generally speaking, resources are passed by reference in PHP 5 by default. So having any references should not be required, and you should never open up more than one database connection unless you really need it.

    Personally, I use a singleton-factory class for my database connections, and whenever I need a database reference I just call Factory::database(), that way I don't have to worry about multiple connections or passing/receiving references.

    connect(DB_HOST, DB_USER, DB_PASS, DB_DATABASE);
            self::$local_db->debugging = DEBUG;
        }
        return self::$local_db;
    }
    }
    ?>
    

提交回复
热议问题