php资源是通过引用传递的吗? [英] Are php resources passed by reference?

查看:41
本文介绍了php资源是通过引用传递的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我今天发现除了对象和原语之外,PHP 还有 资源.文档指出,默认情况下 php 按值传递名称.但是我们知道在 PHP 5 中,对象是通过句柄引用的,并且因此,虽然句柄是按值传递的,但您可以将句柄本身视为引用,巧妙地避免了这个问题.

I discovered today that in addition to objects and primitives, PHP has resources. The documentation states that by default php passes names by value. But we know that in PHP 5, objects are referenced by handle, and so while the handle is passed by value, you can treat the handles as references themselves, neatly avoiding the question.

但是资源呢?它们与对象一样,只是被视为引用本身的句柄,还是它们实际上是在传递时被复制的值?

But what about resources? Are they, like objects, just handles to be treated as references themselves, or are they actually values that get copied when they're passed?

例如:

/**
 * 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);
    }
}

推荐答案

资源不是实际的连接.资源只不过是指向连接的指针.因此,当您关闭属于该资源的连接时,无论是原始连接还是复制连接,行为都没有区别.

A resource is not the actual connection. A resource is nothing more than a pointer to a connection. So when you close the connection belonging to this resource, there is no difference in behavior if it was original or a copied one.

这篇关于php资源是通过引用传递的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆