我今天发现除了对象和原语,PHP 还有资源。文档指出,默认情况下php 按值传递名称。但是我们知道,在 PHP 5 中,对象是由句柄引用的,因此当句柄是按值传递时,您可以将句柄本身视为引用,巧妙地避免了这个问题。
但是资源呢?它们是像对象一样只是句柄本身被视为引用,还是它们实际上是在传递时被复制的值?
例如:
/**
* 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);
}
}