php.net |  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
Votes:1
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
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: ky dot patterson at adlinkr dot com
New email:
PHP Version: OS:

 

 [2014-08-15 00:40 UTC] 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

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-08-15 01:40 UTC] requinix@php.net
-Package: PHP Language Specification +Package: *General Issues
 [2017-08-05 04:56 UTC] stas@php.net
-Status: Open +Status: Suspended
 [2017-08-05 04:56 UTC] stas@php.net
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.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Dec 22 17:01:29 2024 UTC