今天我发现除了对象和原语外,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