Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:97781 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 87150 invoked from network); 16 Jan 2017 09:31:03 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 16 Jan 2017 09:31:03 -0000 Authentication-Results: pb1.pair.com smtp.mail=michal@brzuchalski.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=michal@brzuchalski.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain brzuchalski.com designates 188.165.245.118 as permitted sender) X-PHP-List-Original-Sender: michal@brzuchalski.com X-Host-Fingerprint: 188.165.245.118 ns220893.ip-188-165-245.eu Received: from [188.165.245.118] ([188.165.245.118:47954] helo=poczta.brzuchalski.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id AB/96-00729-2D29C785 for ; Mon, 16 Jan 2017 04:31:01 -0500 Received: from localhost (localhost.localdomain [127.0.0.1]) by poczta.brzuchalski.com (Postfix) with ESMTP id 4A3E62984237 for ; Mon, 16 Jan 2017 10:30:54 +0100 (CET) Received: from poczta.brzuchalski.com ([127.0.0.1]) by localhost (poczta.brzuchalski.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id loInkmehvz2S for ; Mon, 16 Jan 2017 10:30:52 +0100 (CET) Received: from mail-vk0-f43.google.com (unknown [209.85.213.43]) by poczta.brzuchalski.com (Postfix) with ESMTPSA id B43602984233 for ; Mon, 16 Jan 2017 10:30:51 +0100 (CET) Received: by mail-vk0-f43.google.com with SMTP id r136so69721055vke.1 for ; Mon, 16 Jan 2017 01:30:51 -0800 (PST) X-Gm-Message-State: AIkVDXK3axfKFkvpCWAaFW4EuCxf8krFKA7Pu79FC+wdQAFPgq+0NwVQ24h8LJ8Pg90dyHw9FDOEiir+42xqAw== X-Received: by 10.31.155.75 with SMTP id d72mr14517456vke.55.1484559050831; Mon, 16 Jan 2017 01:30:50 -0800 (PST) MIME-Version: 1.0 Received: by 10.103.35.204 with HTTP; Mon, 16 Jan 2017 01:30:50 -0800 (PST) In-Reply-To: References: Date: Mon, 16 Jan 2017 10:30:50 +0100 X-Gmail-Original-Message-ID: Message-ID: To: Marco Pivetta Cc: Wes , PHP Internals Content-Type: multipart/alternative; boundary=001a1141ddd2df82c8054632d621 Subject: Re: [PHP-DEV] Unsetting declared fields From: michal@brzuchalski.com (=?UTF-8?Q?Micha=C5=82_Brzuchalski?=) --001a1141ddd2df82c8054632d621 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable As I am familiar with those interceptions, I tend to point out some dirty hacks when reflection tells you property exists while getting notice on set, see https://3v4l.org/VDMHm bar); } } $foo =3D new FooHack(new Foo); $reflectionFooBar =3D new \ReflectionProperty(Foo::class, 'bar'); var_dump((new ReflectionClass(Foo::class))->getProperties()); var_dump($reflectionFooBar->getValue($foo)); var_dump( property_exists(FooHack::class, 'bar'), property_exists($foo, 'bar') ); Funny PHP7 has different behaviour from 7.0.7 - 7.1 with raising a notice. I may be just looking for dirty example without purpose right now so, don't listen to me. But I do feel like this could bring someone crazy when something should exists while it's not. Those property_exists($foo, 'bar') shouldn't return false in that example?! 2017-01-16 10:17 GMT+01:00 Marco Pivetta : > On Mon, Jan 16, 2017 at 9:49 AM, Micha=C5=82 Brzuchalski < > michal@brzuchalski.com> 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 stil= l >> 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 caus= e >> 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 1= 0;) >>> 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 >>> allowed >>> > for fields that are explicitly declared. I think unset() should set t= he >>> > 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 peopl= e >>> not >>> > doing nasty stuff with our code. This might sound ironic it is >>> actually 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/ > cce5477857504997baf3168974b8f1283516a686/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/ > --=20 regards / pozdrawiam, -- Micha=C5=82 Brzuchalski about.me/brzuchal brzuchalski.com --001a1141ddd2df82c8054632d621--