Passing database connection by reference in PHP

后端 未结 7 2187
清歌不尽
清歌不尽 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 10:05

    Generally speaking, references are not faster in PHP. It's a common misconception, because they are semantically similar to C pointers, so people familiar with C often assume they work the same way. Not so. In fact, references are a tiny bit slower than copies, unless you actually assign to the variable (Which in any case is bad style, unless the variable is an object).

    PHP has a mechanism called copy-on-write, which means that a variable isn't actually copied before it needs to. You can pass a huge data structure to a function; As long as it just reads from it, it makes no difference. A reference however, needs an additional entry in the internal registers, so it would actually take some extra processing (Though barely noticeable).

    0 讨论(0)
提交回复
热议问题