phpphp-internals

Why increasing refcount of zval not working?


I'm dumping a zval container running on PHP version 7.3.5 (opcache is activated and PHP is thread safe enabled) following this given code :

<?php 
$a = "new string";
$b = $a;
xdebug_debug_zval( 'a' );

accordingly to php.net documentation Example #3 Increasing refcount of a zval it outputs refcount=2 :

a: (refcount=2, is_ref=0)='new string'

On my setup (php7.3.5), it outputs refcount=1 :

a:
(refcount=1, is_ref=0)string 'new string' (length=10)

I actually reach the same output with PHP5.6 as the php.net documentation. What internally changed in PHP7 and why refcount remain 1 ?


Solution

  • As pointed out in comments by Nikita, the string, as a constant literal, is interned - which is an internal term meaning something similar to "singleton for strings" - and so is not reference counted.

    As well as interned strings, immutable arrays - which are arrays whose shape and location in memory may not be altered during runtime - are also not reference counted.

    Xdebug 3.0 will be updated with this patch, which will allow it to show when a string is interned, or an array is immutable.