Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:97780 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 84885 invoked from network); 16 Jan 2017 09:18:10 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 16 Jan 2017 09:18:10 -0000 Authentication-Results: pb1.pair.com header.from=ocramius@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=ocramius@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 74.125.82.50 as permitted sender) X-PHP-List-Original-Sender: ocramius@gmail.com X-Host-Fingerprint: 74.125.82.50 mail-wm0-f50.google.com Received: from [74.125.82.50] ([74.125.82.50:38194] helo=mail-wm0-f50.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 64/26-00729-ECF8C785 for ; Mon, 16 Jan 2017 04:18:08 -0500 Received: by mail-wm0-f50.google.com with SMTP id r144so165710185wme.1 for ; Mon, 16 Jan 2017 01:18:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=W4S2VhIGwHig/DKaS4o5zIsGkLpcfxZMGqioQeWiZZg=; b=jx6I7ROIi8rohKLX08pDbWrgpBpHcuetO+cbHOAdrPzzyW1yaZhe5y97j1SAGIaq9D eKoBuwAsFKaVDGYzJDri3s73xz9YZ0aV5riMIHPBwDm9VT9LQzGCrPBWa2Z0CI/VPlrg Xl1g+esHBoCf0sX4Id1lnFSbTWLAK/1yjsD/hG2R5P0CeUcpoh/ocFXqU8KN3O0jfWUR IPgzaca487yTkmkRlHNbWXZwHjNZ2VGewTAA+zDf1Y03f3FdaXHsIBfAySzXAO1Tjv49 cJ2pT1zggX9XfCsgm9AkgnBb9hNELEOMNEvNX6wbVVgXxCOndUJHjEZV+W1TkJoJa3sH OtnA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=W4S2VhIGwHig/DKaS4o5zIsGkLpcfxZMGqioQeWiZZg=; b=R/7rbka/WHDuJTUfxfvCiZH+dH6+G9B/1sDV+sEOm1KYv6+DtC42WIUVmXD+vUtOtx KftR9IIROyNK791/tw7ml2XHZW2SHJ4aJ+QOdD+L4N1tHAEOng+3plI6ydsfwmaYXWMH HXO7ohORnwj9C6PgF46HQw5EFM3bHzGyu+regxOdaoMhDONZ9X3n6cv5ldEszuzpFgK8 5Dn84LhrYbDDslWOS/vUDsOetWr7f1ad4WFkej9weELTd0WDH8tD9e1R4jYc8lRscGAJ gR2cN9CynqeLFIlIkrHKGSnLcoaktnvMVAombwhT3YhANg4YWj+bCf+U05DtEgDSn6/Z uYaA== X-Gm-Message-State: AIkVDXLNflUj9ShwV+i1HIon11V2iJhnZp0iv7Yk2JNRO1RqNlu87pP0aTnwDekmhbphNbosXHwSXoXy4y92/g== X-Received: by 10.28.129.147 with SMTP id c141mr12331499wmd.12.1484558283366; Mon, 16 Jan 2017 01:18:03 -0800 (PST) MIME-Version: 1.0 Received: by 10.194.34.197 with HTTP; Mon, 16 Jan 2017 01:17:42 -0800 (PST) In-Reply-To: References: Date: Mon, 16 Jan 2017 10:17:42 +0100 Message-ID: To: =?UTF-8?Q?Micha=C5=82_Brzuchalski?= Cc: Wes , PHP Internals Content-Type: multipart/alternative; boundary=001a11423eec20e850054632a914 Subject: Re: [PHP-DEV] Unsetting declared fields From: ocramius@gmail.com (Marco Pivetta) --001a11423eec20e850054632a914 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable On Mon, Jan 16, 2017 at 9:49 AM, Micha=C5=82 Brzuchalski wrote: > Hi Marco, > > 2017-01-16 0:27 GMT+01:00 Marco Pivetta : > >> Hi Wes, >> >> This has been discussed before, and it's currently used to intercept >> access >> to properties. Since we don't have property accessors (sigh), the code >> (simplified version) would look like following: >> >> class Foo >> { >> public $bar =3D 'baz'; >> } >> >> class FooInterceptor extends Foo >> { >> private $wrapped; >> public function __construct(Foo $wrapped) >> { >> $this->wrapped =3D $wrapped; >> unset($this->bar); >> } >> public function __get(string $name) >> { >> var_dump('reading ' . $name); >> return $this->wrapped->$name; >> } >> } >> >> $foo =3D new FooInterceptor(new Foo); >> >> var_dump($foo->bar); >> >> You can see a working example at https://3v4l.org/UtugD > > > There is one more thing might be confusing - reflection tells there still > exists bar property after unset while it's realy not. > For example https://3v4l.org/NAg1l > > $class =3D new ReflectionClass(FooInterceptor::class); > $property =3D $class->getProperty('bar'); > var_dump($property); // still exists while actually being unset may cause > errors > > I'm sticking to extending class without magic _get method implemented. > > >> >> >> This behavior is protected from regressions since PHP 5.4, but has been >> working since 5.0: >> https://github.com/php/php-src/blob/cd2b462a2742c79256668d47 >> 36644e34573c33d9/tests/classes/unset_properties.phpt >> >> We can most probably get rid of this weird behavior once property >> accessors >> are in the language. >> >> Greets, >> >> Marco Pivetta >> >> http://twitter.com/Ocramius >> >> http://ocramius.github.com/ >> >> On Mon, Jan 16, 2017 at 12:20 AM, Wes wrote: >> >> > Hello elephpants. >> > >> > Currently PHP allows explicitly declared fields (eg public $foo =3D 10= ;) >> to >> > be removed entirely through unset (eg unset($obj->foo)). >> > >> > Now that isn't really an issue as properties in php are currently >> untyped >> > and therefore nullable; at worst you would get a notice. But it would >> > become an issue with typed fields... that might get a second chance >> sooner >> > or later. >> > >> > But regardless of that, it looks very strange to me that this is allow= ed >> > for fields that are explicitly declared. I think unset() should set th= e >> > field to null if it's declared in the class, and remove the field >> > altogether only if it was defined dynamically. >> > >> > On the other hand, this is just one of many ways of hacking php that >> just >> > exist and we accept / don't care because we have faith in other people >> not >> > doing nasty stuff with our code. This might sound ironic it is actuall= y >> not >> > :P >> > >> > However, I am curious: what you think about this? Should PHP do >> something >> > in regard? Should this continue to work like it does now? Why do you >> feel >> > it should do the one or the other? >> > >> > Hi Micha=C5=82, Reflection will also trigger `__get` in this scenario, which is expected and was also reverted multiple times in "fixes" that worked around or forgot to call the property access guards. class Foo { public $bar =3D 'baz'; } class FooInterceptor extends Foo { private $wrapped; public function __construct(Foo $wrapped) { $this->wrapped =3D $wrapped; unset($this->bar); } public function __get(string $name) { var_dump('reading ' . $name); return $this->wrapped->$name; } } $foo =3D new FooInterceptor(new Foo); $reflectionFooBar =3D new \ReflectionProperty(Foo::class, 'bar'); var_dump($reflectionFooBar->getValue($foo)); See https://3v4l.org/6JtWT for a working example. You can see https://github.com/Ocramius/ProxyManager/tree/cce5477857504997baf3168974b8f= 1283516a686/tests/language-feature-scripts for As I already mentioned, this hack is currently necessary to make property access interception transparent, which is common for most AOP-oriented code. We need an alternate approach to make this happen, before such a feature can be dropped. Marco Pivetta http://twitter.com/Ocramius http://ocramius.github.com/ --001a11423eec20e850054632a914--