Gday internals

Today I felt over a topic while looking at some documentation bugs,
and as the title says its the Resource constants. From what I could
understand by asking Felipe and reading over some bug reports and such
then resource constants aren't really supported. I had a play around
with it and rand into some "WTF"'s I wanted to get information on so I
can upgrade the documentation for this.

Its possible to use resource constants and they work all perfect for
the quick overview I did of them, but the following things might
confuse our users:

Issue #1:
<?php
define('FP', fopen('/home/kalle/myfile.txt', 'r'));

// ...

fclose(FP);
?>

The value of FP is not an Resource of unknown, which is logical enough
from when working with variables. However when you're working with
variables you might do:
<?php
fclose($fp);
$fp = NULL;
?>

So the value is unset, so we're sure the resource is closed, however
since constants are constants the value will remain that Unknown
resource, so to check if the resource still is of that type we want
(when checking if the resource is active) we need to have a
conditional with a call to get_resource_type() and then match it for
that type we now wanted to check if it was. I belive if that was
changed to NULL it would solve some wondering when debugging your
application.


Issue #2:
<?php
$fp = fopen('/home/kalle/myfile.txt', 'r');
define('FP', $fp);

// ...
?>

Now which should we close using fclose? Calling fclose on the variable
will NOT close the connection, however calling it on the constant will
close it and change it to resource unknown as well as the variable.
Which also may leed to confusion between people who does that. (I'd
like to let people do whatever they want, and not let us deside how to
design the code, so either this is a bug or because its not "really"
supported). This might be a reference issue or something similar, I
didn't look much into the cause of this.


So, I propose its either being a "supported" feature, or simply put an
deprecation notice on it (5.3) and remove it HEAD. I personally vote
for the last option, as I don't think resources should be constants as
they do not have the constant value even though they do on some level.


References:
http://bugs.php.net/bug.php?id=45982
http://bugs.php.net/bug.php?id=46348
http://google.com/codesearch?hl=pt-BR&lr=&q=lang%3Aphp+define%5C%28%5B%5E%2C%5D%2B%2C%5Cs%2A%28mysqli%3F%7Cpg%29_connect%5C%28&sbtn=Pesquisar


-- 
Kalle Sommer Nielsen

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to