|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #67847 add language construct is_ref() and refcount()
Submitted: 2014-08-15 00:40 UTC Modified: 2017-08-05 04:56 UTC
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: ky dot patterson at adlinkr dot com Assigned:
Status: Suspended Package: *General Issues
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2014-08-15 00:40 UTC] ky dot patterson at adlinkr dot com
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): Request for refcount function Add var_ref_count() Missing function to determine refcount of variables directy 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";
    echo "\$a is not a reference\n";
echo "\$a refcount is " . getrefcount($a) . "\n";

if (is_ref($d))
    echo "\$d is a reference\n";
    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


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2014-08-15 01:40 UTC]
-Package: PHP Language Specification +Package: *General Issues
 [2017-08-05 04:56 UTC]
-Status: Open +Status: Suspended
 [2017-08-05 04:56 UTC]
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 ( as an RFC. Please read the guide about creating RFCs here:
If you haven't had experience with writing RFCs before, it is advised to seek guidance on the Internals list ( 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.
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Thu Oct 21 23:03:33 2021 UTC