[PHP-BUG] Req #67847 [NEW]: add language construct is_ref() and refcount()

[email protected] ("ky dot patterson at adlinkr dot com")
Newsgroups php.standards
Message-ID <[email protected]>
From:             ky dot patterson at adlinkr dot com
Operating system: 
PHP version:      Irrelevant
Package:          PHP Language Specification
Bug Type:         Feature/Change Request
Bug description:add language construct is_ref() and refcount()

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 bug report at https://bugs.php.net/bug.php?id=67847&edit=1
--
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.