今天我發現除了對象和原語外,PHP還有resources。該文件指出,默認php passes names by value。但是我們知道,在PHP 5,objects are referenced by handle等等中,雖然句柄是按值傳遞的,但您可以將句柄作爲引用自己處理,整齊地避免了這個問題。是否通過引用傳遞了php資源?
但資源?它們是否像對象一樣只是處理爲引用本身,還是它們實際上是在傳遞時被複制的值?
例如:
/**
* Close the ftp connection and throw an exception.
*
* @hack Because php doesn't have a `finally` statement,
* we workaround it to make sure the ftp connection is closed.
* @param resource $conn FTP Buffer
* @param Exception $e
*/
function ftpCloseWithException($conn, $e) {
ftp_close($conn); // <-- Is this the same FTP Buffer resource or a new one?
throw $e;
}
/**
* Copy the README file from ftp.mozilla.org or do something equally arbitrary using ftp.
*/
function getMozReadme() {
try {
$conn = ftp_connect('ftp.mozilla.org');
…
} catch (Exception $e) {
ftpCloseWithException($conn, $e);
}
}
資源總是通過引用 – 2013-05-01 14:55:48
資源是引用 – 2013-05-01 14:56:59