title: Are php resources passed by reference?
body: <p>I discovered today that in addition to objects and primitives, PHP has <a href="http://php.net/manual/en/language.types.resource.php" rel="noreferrer"><em>resources</em></a>. The documentation states that by default <a href="http://php.net/manual/en/functions.arguments.php#functions.arguments.by-reference" rel="noreferrer">php passes names by value</a>. But we know that in PHP 5, <a href="http://www.php.net/manual/en/migration5.oop.php" rel="noreferrer">objects are referenced by handle</a>, and so while the handle is passed by value, you can treat the handles as references themselves, neatly avoiding the question.</p>
  
  <p>But what about <em>resources</em>? Are they, like objects, just handles to be treated as references themselves, or are they actually values that get copied when they're passed?</p>
  
  <p>For example:</p>
  
  <pre><code>/**
   * 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); // &lt;-- 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);
      }
  }
  </code></pre>