php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #66455 Missing function to determine refcount of variables directy
Submitted: 2014-01-10 08:43 UTC Modified: 2015-04-07 17:07 UTC
From: leuffen at continue dot de Assigned:
Status: Duplicate Package: Variables related
PHP Version: 5.5.8RC1 OS: Linux
Private report: No CVE-ID: None
 [2014-01-10 08:43 UTC] leuffen at continue dot de
Description:
------------
While working on an Entity-Manager Class I noticed PHP has no function to determine the number of active references to a variable/object (like debug_zval_dump() does).

It would be great if PHP provided that feature in later versions.






Test script:
---------------
/**
 * function to do the job:
 *
 */
function refcount ($var) {
    ob_start();
    debug_zval_dump($var);
    $zval = ob_get_clean();

    preg_match ("/refcount\\(([0-9]+)\\)/", $zval, $matches);
    return $matches[1];
}



Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-08-15 03:42 UTC] ky dot patterson at adlinkr dot com
As I understand it, this cannot be done with a function.
debug_zval_dump() does not itself return accurate results.

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:
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

I *think* the solution is actually to add new language constructs, and I have opened a bug to discuss that:
https://bugs.php.net/bug.php?id=67847
 [2015-04-07 17:07 UTC] nikic@php.net
-Status: Open +Status: Duplicate
 [2015-04-07 17:07 UTC] nikic@php.net
Marking as duplicate, per ky's last comment.
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Tue May 21 22:01:26 2019 UTC