Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:128334 X-Original-To: internals@lists.php.net Delivered-To: internals@lists.php.net Received: from php-smtp4.php.net (php-smtp4.php.net [45.112.84.5]) by lists.php.net (Postfix) with ESMTPS id 72A321A00BC for ; Thu, 31 Jul 2025 08:47:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1753951569; bh=SgxJeyg9MZfXCuEfkmdAAPB478+qPcVdOHaWv/tqctU=; h=Date:From:To:Subject:From; b=KUEU+5e74MegbtAgLsk6SBelWNR8IyoMHSnzndztZJR/v5MOADsDnbcSk8xZ2uI11 Bmd0zeVzOnKhPeY8aqNHHlYyaxRdnabInfsAe/V+gVWhQt2vaU2eDgL4H0Fdg5Zz9E 4V6vJe6vK2Y1svsGJk3ZHDhizQJFh2Q0NrWydu0MKQ7wf/4muNcIjR8bYlsmTqUKhh NrLEHJeZwZmVjvojmrdKQ3JWnW/cogsznFFFP2UT800lEF45SvExw7oVTnMldIjzVQ f8WjJ+t1Hx3nz/JaEhqAQFr534ETdUtJclcQhNBhLQvIDZmrMdTtf/KlE5VeCiUO4U IFnnNo3hyOefA== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 15CAC180086 for ; Thu, 31 Jul 2025 08:46:08 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_MISSING,HTML_MESSAGE, RCVD_IN_DNSWL_LOW,SPF_HELO_PASS,SPF_PASS autolearn=no autolearn_force=no version=4.0.1 X-Spam-Virus: Error (Cannot connect to unix socket '/var/run/clamav/clamd.ctl': connect: Connection refused) X-Envelope-From: Received: from fhigh-a6-smtp.messagingengine.com (fhigh-a6-smtp.messagingengine.com [103.168.172.157]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Thu, 31 Jul 2025 08:46:07 +0000 (UTC) Received: from phl-compute-05.internal (phl-compute-05.phl.internal [10.202.2.45]) by mailfhigh.phl.internal (Postfix) with ESMTP id 79D9B140027F for ; Thu, 31 Jul 2025 04:47:49 -0400 (EDT) Received: from phl-imap-05 ([10.202.2.95]) by phl-compute-05.internal (MEProxy); Thu, 31 Jul 2025 04:47:49 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bottled.codes; h=cc:content-type:content-type:date:date:from:from:in-reply-to :message-id:mime-version:reply-to:subject:subject:to:to; s=fm1; t=1753951669; x=1754038069; bh=bbASzUKwNNfAR1QMqDivx9Oi/dgjGn34 zYYEBf1+esg=; b=G0Xa4CaLXrb9kKt98eLQdEzzN7SQbbGhBGHq/mwFwAvZm3HI C1IuqSUe1kc101EOPJNb2Bu27Np64+KHP4VY293zDZMDHMlN9poDZt5vVgsx5fJZ e69Hdw22SiidZX7UupJARY1HWmxfI7T0cZoq/LJC1L2vr7Opv2Jlx8EutdpVVJij iTIreCXSK2RPFiRcOYgtJv7ii4FjMGHLz0lWo/KIGCB72jP14iGJtAXR/EDB1zBs H/kel7X3dA1j6jr4wMa7hLdfCgT9P4yxDFpbtp5+y82BQSJAJ7iPk8gri6OoLXiC SjtwzVWoyQDxXZdD0reKb5o/5MK3Lprur9/dkQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:message-id :mime-version:reply-to:subject:subject:to:to:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm3; t=1753951669; x= 1754038069; bh=bbASzUKwNNfAR1QMqDivx9Oi/dgjGn34zYYEBf1+esg=; b=j jAH4su7Rk7ia6Ln+PAgCP0WG/fn7ZfANFNGAui64WU7wK0bSEIrbrCjTJELq8NWe MKVO7P7QyLDBWP9QUA1fNgCNoIbXJ5eIiH0uFotNHNtE6z4TWvh56RHVPCFhLpkC w+ANTYNb0Hjl+CfU28evxQ9tuiZUug/n1+Tvc+uX1zIigW0Q3bgVwnBtue4lQnoZ HCTmi450VceY7ulT6eNiTCw+SQw4o5cvt9dVhfSAqqM/oPWht+bwCAZ6c74xmzXx jVBlwLZDMeuiaPnJNSz+lI0GODN6NOv3jjOh6mHROMx98atW+olWd1zZjf+MuBmg bMzQ4Vl4+wK1/ld/vZ2eg== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeffedrtdefgddutddtfeejucetufdoteggodetrf dotffvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfurfetoffkrfgpnffqhgenuceu rghilhhouhhtmecufedttdenucgoufhushhpvggtthffohhmrghinhculdegledmnecujf gurhepofggfffhvffkufgtsegrtderreertdejnecuhfhrohhmpedftfhosgcunfgrnhgu vghrshdfuceorhhosgessghothhtlhgvugdrtghouggvsheqnecuggftrfgrthhtvghrnh epleeukeefudegleekueduveegudeiveejvdeluedtveffheehvdelleeuieeiveeunecu ffhomhgrihhnpeefvheglhdrohhrghenucevlhhushhtvghrufhiiigvpedtnecurfgrrh grmhepmhgrihhlfhhrohhmpehrohgssegsohhtthhlvggurdgtohguvghspdhnsggprhgt phhtthhopedupdhmohguvgepshhmthhpohhuthdprhgtphhtthhopehinhhtvghrnhgrlh hssehlihhsthhsrdhphhhprdhnvght X-ME-Proxy: Feedback-ID: ifab94697:Fastmail Received: by mailuser.phl.internal (Postfix, from userid 501) id 0EC571820074; Thu, 31 Jul 2025 04:47:49 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net x-ms-reactions: disallow MIME-Version: 1.0 Date: Thu, 31 Jul 2025 10:46:50 +0200 To: internals@lists.php.net Message-ID: <1d8574f9-8171-4398-96d5-70058deaccb0@app.fastmail.com> Subject: [PHP-DEV] references & arrays + hooks Content-Type: multipart/alternative; boundary=a97984a104cd40858afd7a438c54bc8a From: rob@bottled.codes ("Rob Landers") --a97984a104cd40858afd7a438c54bc8a Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Hello internals: Currently, this is not allowed by PHP: class Foo { public function __construct( public string &$ref { get =3D> $this->ref ?? 'null'; set =3D> $thi= s->ref =3D $value; } ) {} } $var =3D 'hello'; $foo =3D new Foo($var); $var =3D "world"; echo $foo->ref; https://3v4l.org/JRArK Currently, you have to emulate it like so: class Foo { public string $ref { get =3D> $this->_ref; set =3D> $this->_ref =3D $value; } public function __construct( private string &$_ref, ) {} } $var =3D 'hello'; $foo =3D new Foo($var); $foo->ref =3D 'world'; echo $var; https://3v4l.org/0cC0S After the release, I'd like to propose an RFC that allows using referenc= es with hooks: A hooked reference variable is a callable/closure under the hood (techni= cally, a reference to the actual hook methods) that can be used like a v= alue. Reading from it calls the ::get() method, and setting to it calls = the ::set($value) method. If the callable were to be written in PHP, it = might look something like the following psuedo code: function hookRef(...$args) use (&$backedValue) { if(empty($args)) return $backedValue::get(); $backedValue =3D $backedValue::set(array_first($args)); } In simplified terms, the engine, upon detecting a read/write to a hooked= reference (eg, a standalone variable pointing to a hooked property), in= stead of doing typical variable access, it will call the bound closure w= ith the appropriate arguments. This will effectively allow hooks to be r= eferences. This would also handle the case for arrays, which appears to require the= same plumbing to exist. Downsides: 1. this will probably necessitate a new zval type, determined at runtim= e (it will not be exposed to users). 2. there are a lot of ways to access/write to a variable (especially on= ce you consider arrays) thus it will touch a lot of the engine. Thoughts? Concerns? Suggestions? =E2=80=94 Rob --a97984a104cd40858afd7a438c54bc8a Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: quoted-printable
Hello intern= als:

Currently, this is not allowed by PHP:

class Foo {
  public function __con= struct(
      public string &$ref { get =3D= > $this->ref ?? 'null'; set =3D> $this->ref =3D $value; }
  ) {}
}

$var =3D 'hello= ';
$foo =3D new Foo($var);
$var =3D "world";

echo $foo->ref;

Currently,= you have to emulate it like so:

class Foo {

  public string $ref {
  = ;    get =3D> $this->_ref;
  &n= bsp;   set =3D> $this->_ref =3D $value;
 = }

  public function __construct(
      private string &$_ref,
&n= bsp; ) {}
}

$var =3D 'hello';
$foo =3D new Foo($var);
$foo->ref =3D 'world';

echo $var;
https://3v4l.org/0cC0S

After the release, I'd like to propose an RFC that = allows using references with hooks:

A hooked re= ference variable is a callable/closure under the hood (technically, a re= ference to the actual hook methods) that can be used like a value. Readi= ng from it calls the ::get() method, and setting to it calls the ::set($= value) method. If the callable were to be written in PHP, it might look = something like the following psuedo code:

funct= ion hookRef(...$args) use (&$backedValue) {
  if(empt= y($args)) return $backedValue::get();
  $backedValue =3D = $backedValue::set(array_first($args));
}

<= div>In simplified terms, the engine, upon detecting a read/write to a ho= oked reference (eg, a standalone variable pointing to a hooked property)= , instead of doing typical variable access, it will call the bound closu= re with the appropriate arguments. This will effectively allow hooks to = be references.

This would also handle the case = for arrays, which appears to require the same plumbing to exist.

Downsides:
  1. this will probably necessi= tate a new zval type, determined at runtime (it will not be exposed to u= sers).
  2. there are a lot of ways to access/write to a variable (es= pecially once you consider arrays) thus it will touch a lot of the engin= e.
Thoughts? Concerns? Suggestions?

=E2=80=94 Rob --a97984a104cd40858afd7a438c54bc8a--