Re: Introspection for references
| From: | Nicolas Grekas | Date: | Sun, 08 Jul 2018 08:42:08 +0000 |
| Subject: | Re: Introspection for references | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-102646@lists.php.net to get a copy of this message | ||
Hi Nikita,
Before talking about solutions, can the people who need this first outline
> what functionality is needed and what it is needed for (and maybe what
> workarounds you currently use). E.g. do you only need to know whether
> something is a reference, or do you need to know whether two somethings are
> part of the same reference, etc. There are probably multiple use cases for
> this with different needs.
>
We're using reference introspection to do both: we need to know when a zval
is a reference, and we also need to track each of them separately.
The use case is being able to intropect any arbitrary PHP datastructure,
with one main application: providing an enhanced "dump()" function.
See e.g. this screenshot for what we get using the dump() function provided
by Symfony VarDumper component:
https://symfony.com/doc/current/_images/07-hard-ref.png
In PHP5 days, Julien Pauli wrote a PHP extension to do zval introspection.
Here is the code + README (see test case 001.phpt for example with
references.):
https://github.com/symfony/symfony/tree/3.4/src/Symfony/Component/Debug/Resources/ext
With PHP7, using pure PHP introspection is easier to maintain and still
very fast so we deprecated the extension.
Here is the code doing reference introspection:
https://github.com/symfony/symfony/blob/master/src/Symfony/Component/VarDumper/Cloner/VarCloner.php#L83
it might not be easy to follow, but the basic blocks are:
$array2 = $array1;
$array2[$key] = $unique_cookie;
if ($array1[$key] === $unique_cookie) => we found a reference
then we also maintain a registry of $unique_cookie so that we know if we
already saw that reference or not (the check is done before the above "if"
or course.)
Cheers,
Nicolas