Newsgroups: php.internals
Path: news.php.net
Xref: news.php.net php.internals:102646
Return-Path: <nicolas.grekas@gmail.com>
Mailing-List: contact internals-help@lists.php.net; run by ezmlm
Delivered-To: mailing list internals@lists.php.net
Received: (qmail 1931 invoked from network); 8 Jul 2018 08:42:34 -0000
Received: from unknown (HELO lists.php.net) (127.0.0.1)
  by localhost with SMTP; 8 Jul 2018 08:42:34 -0000
Authentication-Results: pb1.pair.com smtp.mail=nicolas.grekas@gmail.com; spf=pass; sender-id=pass
Authentication-Results: pb1.pair.com header.from=nicolas.grekas@gmail.com; sender-id=pass
Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.218.51 as permitted sender)
X-PHP-List-Original-Sender: nicolas.grekas@gmail.com
X-Host-Fingerprint: 209.85.218.51 mail-oi0-f51.google.com  
Received: from [209.85.218.51] ([209.85.218.51:34310] helo=mail-oi0-f51.google.com)
	by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP
	id 0E/88-55607-77EC14B5 for <internals@lists.php.net>; Sun, 08 Jul 2018 04:42:31 -0400
Received: by mail-oi0-f51.google.com with SMTP id 13-v6so30764116ois.1
        for <internals@lists.php.net>; Sun, 08 Jul 2018 01:42:31 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20161025;
        h=mime-version:sender:in-reply-to:references:from:date:message-id
         :subject:to:cc;
        bh=W0HzVQ6+NCFbMg+aiIXWQ0IKOj4YXNoac1Ly5i4uo78=;
        b=RXUYs3kTOmBtnEffMjMP4OGiC1GJ3Bb8SRzr1W0yPLtPoAnx3rI1NFqIyX+xrlpGVK
         4trCQq/GdU26P1Ks1L4T7fZ2GAsMI8M0vPsqWKejt+i1BXga0Q42eQ28sNBxqaHtKecz
         FUkjebr2Epz4Yzf2rN1MqifmvexRryDvkoWKUUpV1Di4dMetMT7LecxtCQpxAsGOw1d5
         JY5czUjuoe/je0noplHKQvYkvflXhFtg6mwqUevMHIH79IVj77btEMuMMI6OANrf4ZQz
         KOE8+Wo8zj8qPWeoXKYE39TDG5pjIDMlZciplZ2zu5kF1woHzhK1V2KYLvkPIdrCza1k
         huYg==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20161025;
        h=x-gm-message-state:mime-version:sender:in-reply-to:references:from
         :date:message-id:subject:to:cc;
        bh=W0HzVQ6+NCFbMg+aiIXWQ0IKOj4YXNoac1Ly5i4uo78=;
        b=tmoyY4m5GFOrMwJ7T/p/NhJkjOubcWs9XEPMRsuMF42Gx/iYXB5A4oDxJmDQBWIzwT
         ko0gqOMf/fKWTeHrFSTSuOj/7OnDMjUoq7kQD8No+a5BBqL0GRzgfgQHu4Wdw47ZUPMh
         wJJ9qDhHZQLzB8+GeTypX7iiBw9gMhqLnzF7v3+YywMMffxFp+0asyuC7Pce3BqK+y4s
         F5/kuMQ314w3ycQZQhYFjaeWvZxan+RRCopi52DnrZ6Yh7YHnku4He0BzJY3jJjsIBfk
         PqVFMlcIoMgxdA4eKACCIrm32GXwauADNwN1KJS+bsIpIWgVAydLf9L4tcSrW+BNpdFL
         8PAQ==
X-Gm-Message-State: APt69E01U+qeRllSXiywFRy9d52ApXCdXBXmZfq2sw5qaCX2uWrUDOC8
	pIIWDa3cWeQ6Z8bdZ7f06mq23Ua1l/Umol5CpRY=
X-Google-Smtp-Source: AAOMgpfHWF3qwkWnq2AVPFMyRHHxLQEZp4HE55Bwb8JddxTYRa5Oqja/fny/wMEHxqBsuQ41LepyYAYkzA1EmeI2YLI=
X-Received: by 2002:aca:c70f:: with SMTP id x15-v6mr19259265oif.97.1531039348897;
 Sun, 08 Jul 2018 01:42:28 -0700 (PDT)
MIME-Version: 1.0
Sender: nicolas.grekas@gmail.com
Received: by 2002:a4a:1e45:0:0:0:0:0 with HTTP; Sun, 8 Jul 2018 01:42:08 -0700 (PDT)
In-Reply-To: <CAF+90c8k2rmh8V7yfKOFA57ft1cNjz3DeT-V1LHsnaJnn0XEKg@mail.gmail.com>
References: <CAF+90c8k2rmh8V7yfKOFA57ft1cNjz3DeT-V1LHsnaJnn0XEKg@mail.gmail.com>
Date: Sun, 8 Jul 2018 10:42:08 +0200
X-Google-Sender-Auth: dcxu3EKx01IZ9kBSQ47eW7BaV8s
Message-ID: <CAOWwgpn-7psj9HCFzEJgRg1FvyC7V+AYK5bduNoWgdC8q+fJCg@mail.gmail.com>
To: Nikita Popov <nikita.ppv@gmail.com>
Cc: PHP internals <internals@lists.php.net>
Content-Type: multipart/alternative; boundary="000000000000873dbf057078e099"
Subject: Re: [PHP-DEV] Introspection for references
From: nicolas.grekas+php@gmail.com (Nicolas Grekas)

--000000000000873dbf057078e099
Content-Type: text/plain; charset="UTF-8"

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

--000000000000873dbf057078e099--