Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:117509 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 23818 invoked from network); 10 Apr 2022 11:59:39 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 10 Apr 2022 11:59:39 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 3D11F180381 for ; Sun, 10 Apr 2022 06:30:53 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,HTML_MESSAGE, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-lf1-f52.google.com (mail-lf1-f52.google.com [209.85.167.52]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Sun, 10 Apr 2022 06:30:52 -0700 (PDT) Received: by mail-lf1-f52.google.com with SMTP id t25so22309593lfg.7 for ; Sun, 10 Apr 2022 06:30:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to; bh=Nghw5p+fZUN3mEChQAEZQODhMoxepp+Ke1d64+CJW80=; b=KQlHe2udKIwM6yoBvQVSmbeogWGqilkm7ZiSiMJF6t2TuUQ/V/WHc3LGY9z7Zoh0rE GANVaWoaJoJTEDNfbXixwKPmJmpK8IwL1vnXgYis4m7Fu5qVcC59D/eNS5FW2aeul53t YgKsTJrKLuY0PYlWrh3b3yTedu7ZPuW92cFxij86iYrlgtBh86vAAS54acKsPtKM/RPk 1SqPwwhloDBNVVM8aNt0ZIPkMPPAKq6A0phSFazC7kG9uKO5wttOCN3XjwJVbMHIs0vB C7dIWIq2VslqBM1UBxd+tibly4UjlpSUEoi/3vpGziHXgOS/KN2SB6NUISFAaWKDjeZE ignQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to; bh=Nghw5p+fZUN3mEChQAEZQODhMoxepp+Ke1d64+CJW80=; b=3wRo1tWq/yFUeeuGe4QLSe5O/wY3wiBcMt1LT0Pbg9xjZoyO17x63hl4VrtWnNdPJW yJZ5U3LaSEgffmhOJlntDzskHeU4JjzYrwqs+lOTtKgUnXseJSBdNVyuaW/NQhbLbvtn nv1AjolzOQ3LU/h4Pm9UyWFl0ni2tb8rFzsqU1tgbjXHj/DYIq2o3kDDuK0Lsm4bIkRB LLhxFgOHgedHZWol5aVUG4OUKeWEdHUMT6G1hLhUfxzReJzGgu19/0L1FrWBqBY6Jljb U94ov2JA4VLDfZP1ZahI2PrywIZXu0o5+3xIDsVAK8pjnMQFiRQM33wGzdBXzAv0d/EA AJGw== X-Gm-Message-State: AOAM532wEju3oRlRtCEK1Yxc3LgpBHKPh0GeoOqRi091kk2VDU20i+dN lSjEW/OIN8A/hqp9OMBiUqvwwbhSV7aKzen0jl/zJt8JxEw= X-Google-Smtp-Source: ABdhPJzhzrsf4l9RKSnOCQ/OuH1tt1Zlt4VpstYMPzCRgbM6YjEMeLtQ7tleSdGxCuVAyQP9dPNd9eC6zpA2Hqu7Ocw= X-Received: by 2002:ac2:4202:0:b0:444:14bf:86dc with SMTP id y2-20020ac24202000000b0044414bf86dcmr18717879lfh.126.1649597450878; Sun, 10 Apr 2022 06:30:50 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Sun, 10 Apr 2022 15:30:39 +0200 Message-ID: To: PHP internals Content-Type: multipart/alternative; boundary="00000000000015039005dc4cd657" Subject: Re: unset a reference of property object From: glash.gnome@gmail.com (Glash Gnome) --00000000000015039005dc4cd657 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Q1: "how to modify this line to reproduce the behavior of PHP" A1: /* {{{ vendor_my_get_debug_info */ static HashTable* vendor_my_get_debug_info(zend_object *object, int *is_temp) { vendor_my *intern =3D ZOBJ_TO_VENDOR_MY(object); HashTable *debug_info=3DNULL, *std_props=3DNULL; *is_temp =3D 1; std_props =3D zend_std_get_properties(object); debug_info =3D zend_array_dup(std_props); zval zdata; if (Z_ISREF(intern->data) && Z_REFCOUNT(intern->data)<2) {// If the reference is no longer used by the PHP-user ZVAL_COPY(&zdata, &Z_REF(intern->data)->val);// hide reference (for compatibility) } else { ZVAL_COPY(&zdata, &intern->data);// show reference (for debugging ?= ??) } zend_hash_str_update(debug_info, "data", sizeof("data")-1, &zdata); return debug_info; } /* }}} */ Q2: "Why do I have to hide that it's a reference ?" A2: For historical( debugging) raison, see : https://github.com/php/php-src/blob/master/ext/standard/var.c#L202 I think it's a bad idea to show & but it doesn't really matter. In my case I think it's best never to show the reference : zval zdata; ZVAL_COPY(&zdata, Z_ISREF(intern->data) ? &Z_REF(intern->data)->val : &intern->data); zend_hash_str_update(debug_info, "data", sizeof("data")-1, &zdata); If I need to debug references/refcount it is better to use gdb's pretty-print feature but no var_dump() Le sam. 9 avr. 2022 =C3=A0 17:51, Glash Gnome a =C3= =A9crit : > Out of politeness I answer myself, > > > PHP_FUNCTION(var_dump) > -> php_var_dump() > -> zend_std_get_properties_for() // Zend object = handler get_properties_for > -> obj->handlers->get_debug_info(obj, &is_temp); // Zend object = handler get_debug_info > -> zend_std_get_properties();// Zend object = handler get_properties > -> rebuild_object_properties() > -> _zend_hash_append_ind() > =3D> ZVAL_INDIRECT(&p->val, ptr); // do the tric= k( Z_TYPE change) > TODO: Find out how to manage the IS_INDIRECT > > So to reproduce the default php behavior I can implement the get_debug_in= fo handler. > > I'm still trying to figure out how to modify this line : > void my_get_debug_info() { > // ... > // Z_REFCOUNT(intern->data)=3D=3D1 > // Z_TYPE(intern->data)=3D=3DIS_REFERENCE > // Z_TYPE(Z_REF(intern->data)->val)=3D=3DIS_ARRAY > zval zdata; ZVAL_COPY(&zdata, &intern->data);// Why do I have to hide= that it's a reference ? > zend_hash_str_update(debug_info, "data", sizeof("data")-1, &zdata); > // ... > return debug_info; > } > > my_handlers.get_debug_info =3D my_get_debug_info; > > > struct _my {// intern structure > zval data;// array of unsigned int > zend_object std; > }; > > Le sam. 9 avr. 2022 =C3=A0 15:42, Glash Gnome a = =C3=A9crit : > >> Hello, >> >> I am developing a PHP extension. I can't reproduce the behavior of >> references of Zend/PHP. >> >> Here is some Code illustrating Zend's behavior: >> > class Foo { public $baz =3D array(1);} >> $foo =3D new Foo(); >> $ref =3D &$foo->baz; >> $foo->baz =3D [123];//set array has reference >> var_dump($foo); >> >> Output ------------------------ >> object(Foo)#1 (1) { >> ["baz"]=3D> >> &array(1) { [0]=3D> int(123) } >> } >> -------------------------------- >> Note the symbol ( &) output from the var_dump(); >> >> >> Now I add unset($ref) in my code >> > class Foo { public $baz =3D array(1);} >> $foo =3D new Foo(); >> $ref =3D &$foo->baz; >> //unset($ref);// kill reference >> $foo->baz =3D [123];// set array has reference >> unset($ref) ;// kill reference >> var_dump($foo); >> ------------------------ >> object(Foo)#1 (1) { >> ["baz"]=3D> >> array(1) { [0]=3D> int(123) } >> } >> -------------------------------- >> >> Note that the symbol (&) has disappeared from the output of var_dump(); >> >> I would like to reproduce this behavior but I don't know which method of >> zend_object_handlers allows to remove( Z_UNREF) the reference. >> >> Thank you for your help >> >> >> --00000000000015039005dc4cd657--