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?
For example:
/**
* 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);
}
}
pass by reference. Clearly, PHP, like C++, is a language that does support pass by reference. By default, objects are passed by value.
Pass by reference: When variables are passed by reference, use & (ampersand) symbol need to be added before variable argument. For example: function( &$x ). Scope of both global and function variable becomes global as both variables are defined by same reference.
PHP uses relevent functions to create these resources. For example, fopen() function opens a disk file and its reference is stored in a resource variable. PHP's Zend engine uses reference conting system. As a result, a resource with zero reference count is destroyed automatically by garbage collector.
Definition. Pass by value refers to a mechanism of copying the function parameter value to another variable while the pass by reference refers to a mechanism of passing the actual parameters to the function. Thus, this is the main difference between pass by value and pass by reference.
No they are not passed by reference by default, they are handled as any other PHP variable in this case. Check this example:
function test($fd) {
$fd = NULL;
}
$fd = fopen('/tmp/test', 'w+');
test($fd);
var_dump(is_resource($fd)); // bool(true);
... but it's by the nature of resources they point to a single outer resource. This can be a file, a database connection or something like this. So any operations on a resource (or a copy of it) would have direct effect on that single outer resource.
Check this example:
function close($fd) {
fclose($fd);
}
$fd = fopen('/tmp/test', 'w+');
close($fd);
var_dump(is_resource($fd)); // bool(false);
In the above example, the PHP engine resets the all references to $fd in all scopes after the files has been closed. This means from the sight of this side effect they may not being exactly the same as other variables.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With