Req #67847 [Opn->Sus]: add language construct is_ref() and refcount()
| From: | stas@php.net | Date: | Sat, 05 Aug 2017 04:56:49 +0000 |
| Subject: | Req #67847 [Opn->Sus]: add language construct is_ref() and refcount() | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-210523@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=67847&edit=1
ID: 67847
Updated by: stas@php.net
Reported by: ky dot patterson at adlinkr dot com
Summary: add language construct is_ref() and refcount()
-Status: Open
+Status: Suspended
Type: Feature/Change Request
Package: *General Issues
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
Thank you for your interest in PHP and for submitting a feature request. Please be aware that due to
the magnitude of change this request requires, it would be necessary to discuss it on PHP Internals
list (internals@lists.php.net) as an RFC. Please read the guide about creating RFCs here:
https://wiki.php.net/rfc/howto
If you haven't had experience with writing RFCs before, it is advised to seek guidance on the
Internals list (http://php.net/mailing-lists.php) and/or solicit help from one of the experienced
developers.
Please to not consider this comment as a negative view on the merits of your proposal - every
proposal which requires changes of certain magnitude, even the very successful and widely supported
ones, must be done through the RFC process. This helps make the process predictable, transparent and
accessible to all developers.
Previous Comments:
------------------------------------------------------------------------
[2017-03-21 04:36:55] requinix@php.net
Related To: Bug #74279
------------------------------------------------------------------------
[2014-08-15 00:40:54] ky dot patterson at adlinkr dot com
Description:
------------
Userland developers do not have any way to determine whether a variable (a zval) is a reference, or
to return its reference count.
Function var_dump() shows is_ref.
Function debug_zval_dump() shows is_ref and refcount.
However both functions have severe limitations:
1) both functions output string data, requiring a developer to use output buffering and string
parsing/PCRE to extract the info;
2) since PHP 5.4 calltime pass-by-reference is gone, and neither function is declared to
pass-by-reference so both functions cannot be made to handle references properly even for cases
where the developer knows the value is a reference;
3) "fixing" either function to use pass-by-reference doesn't really solve the
problem, because the zval will still be altered or copied by the act of being passed as a function
argument. By definition, no function can read an unaltered zval.
The Xdebug extension offers xdebug_zval_dump() which does show accurate info, but also has problems:
1) it can't reference all possible variables;
2) it's a non-standard extension, it isn't even on PECL.
As far as I can see, the solution (if there is to be one) is to add new language constructs, similar
to isset() or empty(); e.g.:
bool is_ref(mixed $var)
Return whether PHP variable $var is a reference.
(In other words, return $var's zval's is_ref__gc as a boolean.)
int getrefcount(mixed $var)
Return the reference count of PHP variable $var.
(In other words, return $var's zval's refcount__gc as an int.)
So there are several questions for the PHP core community to consider here, IMO:
1) Am I correct in my assertion that only new language constructs could provide this info for
userland devs?
2) Is it technically feasible to implement these new language constructs?
I am a novice zend hacker at best, I've never read the C code for e.g. empty() so I don't
know what's involved.
3) Is it actually worthwhile to provide this feature?
I think there is good value but I'm just this guy (and I'm not providing a patch to
implement it).
I acknowledge that it's a valid question -- I just think it should be answered conclusively so
the community knows the deal.
Lastly, I found several other bugs (some open) that are essentially asking for this feature but as a
new or improved function (with various comments about how that won't work):
https://bugs.php.net/bug.php?id=40011 Request
for refcount function
https://bugs.php.net/bug.php?id=52215 Add
var_ref_count()
https://bugs.php.net/bug.php?id=66455 Missing
function to determine refcount of variables directy
https://bugs.php.net/bug.php?id=53895
debug_zval_dump should be by ref
Test script:
---------------
// Example script illustrating new language constructs:
$a = 1;
$b = $a;
$c = $b;
$d = 2;
$e =& $d;
if (is_ref($a))
echo "\$a is a reference\n";
else
echo "\$a is not a reference\n";
echo "\$a refcount is " . getrefcount($a) . "\n";
if (is_ref($d))
echo "\$d is a reference\n";
else
echo "\$d is not a reference\n";
echo "\$d refcount is " . getrefcount($d) . "\n";
Expected result:
----------------
$a is not a reference
$a refcount is 3
$d is a reference
$d refcount is 2
Actual result:
--------------
Feature doesn't exist, it is not possible to write code to produce any result
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=67847&edit=1