|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #52215 Add var_ref_count()
Submitted: 2010-07-01 03:33 UTC Modified: 2015-04-07 17:07 UTC
Avg. Score:3.0 ± 0.0
Reproduced:4 of 5 (80.0%)
Same Version:1 (25.0%)
Same OS:2 (50.0%)
From: dev at alepe dot com Assigned:
Status: Duplicate Package: Variables related
PHP Version: 5.3.3RC1 OS: Any
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: dev at alepe dot com
New email:
PHP Version: OS:


 [2010-07-01 03:33 UTC] dev at alepe dot com
It seems there is no easy way to know if an object/array/... is reference. 

Looking at the source code of ext/standard/var.c it seems it may be not so hard to add that function. As debug_zval_dump already outputs the reference count, it would be better to obtain that value in order to determine if an object is referenced or not. Maybe something like (I'm not C programmer):

PHPAPI void php_var_ref_count(zval **struc)
    return Z_REFCOUNT_PP(struc);

Knowing the reference count may be helpful to:
- make copies of a structure without references
- remove variables that have more than 1 reference (safe remove)
- remove variables that are not referenced (unused values)
- prevent modifying a variable that is referenced 

I believe there must be more applications but these are the ones I can think of.


Test script:

$en = array("a" => "apple", "b" => "banana");
$es = array("a" => "manzana", "b" => "platano");
$dict = array(
   "Eng" => $en,
   "Esp" => $es,
   "Non" => array("a" => "A", "b" => "B")

echo var_ref_count($dict["Eng"]); 
echo " # ";
echo var_ref_count($dict["Non"]); 


Expected result:
2 # 1

Actual result:
None (inexistent)


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2010-07-01 03:34 UTC] dev at alepe dot com
Please excuse me if I have some theoretical misconceptions or if my English is not very good.
 [2010-07-01 13:44 UTC]
This won't work, for the same reason that debug_zval_dump() is flawed. By passing a variable to a function, you mess with the refcount/is_ref values. Xdebug's xdebug_debug_zval() ( goes around that by looking up the symbol directly (but it doesn't support array elements directly).
 [2011-05-09 19:07 UTC] thegreatall at gmail dot com
I recently wrote a simple modification to the php source, which returns the 
number of references linked to a variable. It does suffer from the same problem 
that debug_zval_dump suffers from, but it is simply always +1 than what you 
expect. Aadding a note in the documentation saying "The value will always be 1 
more than the actual reference count." or just -1 from the returned value should 

If there's even a chance that this would be reviewed and possibly added into 
php's source, I would be happy to make a patch for it. This function may not 
seem critical, but I developed a framework that does allot of class recycling 
and it would be nice to have the ability to figure out what variables it can 
release, and a function like this or what I wrote would be HUGE in terms of 
memory management. A conversion script I wrote uses upwards of 1-3 gigs of ram 
if I recycle variables, but I could reduce that number to <10 megs if I could 
figure out what variables are free'able. The workaround I have to work with now 
is to either "guess" what isn't being used (which is almost always wrong) or to 
not use any object recycling which is alot slower because it needs to initiate a 
new object every time, and you can have 2 different objects to the same data 
(which the huge downside to it is that there can be 2 objects for the same 
 [2012-09-23 22:09 UTC] chrisw at networkm dot co dot uk
The problem (such as it is) that this issue creates is larger in PHP 5.4. Because 
call-time pass by reference now causes a fatal error, debug_zval_dump() is now 
completely pointless (the data it can provide can obtained from var_dump()). 
Either it should be removed and something like var_ref_count() should be added, or 
it should be altered to take it's argument by reference.
 [2014-08-15 03:39 UTC] ky dot patterson at adlinkr dot com
This cannot be done with a function.

The act of passing a variable to a function (by reference or by value) changes or replaces the variable's zval.
So the refcount you will get is inaccurate -- and not just "off by 1", because zend may copy the zval before it is passed to your function.

There are several other bugs that either ask for a refcount function, or ask for debug_zval_dump to be "fixed" to support pass-by-reference, and all of them are not workable AFAIK: Request for refcount function Add var_ref_count() Missing function to determine refcount of variables directy debug_zval_dump should be by ref

I *think* the solution is actually to add new language constructs, and I have opened a bug to discuss that:
 [2015-04-07 17:07 UTC]
-Status: Open +Status: Duplicate
 [2015-04-07 17:07 UTC]
Marking as duplicate, per ky's last comment.
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Mon Mar 01 13:01:24 2021 UTC