Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:97768 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 46644 invoked from network); 15 Jan 2017 23:30:44 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 15 Jan 2017 23:30:44 -0000 Authentication-Results: pb1.pair.com smtp.mail=ocramius@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=ocramius@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 74.125.82.41 as permitted sender) X-PHP-List-Original-Sender: ocramius@gmail.com X-Host-Fingerprint: 74.125.82.41 mail-wm0-f41.google.com Received: from [74.125.82.41] ([74.125.82.41:36633] helo=mail-wm0-f41.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 1C/B0-00729-2260C785 for ; Sun, 15 Jan 2017 18:30:44 -0500 Received: by mail-wm0-f41.google.com with SMTP id c85so137405765wmi.1 for ; Sun, 15 Jan 2017 15:30:42 -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=0/fHOcsZ/l55nG++5wSq62hyeuwElgewgLEkUE/cyoo=; b=qASCeI1bGwrZpnncw/c8iKzLrNaYy4/cse1ck+CBqOj1IU5YSj5Dd04x/ihX6RSKf7 kGhkIRIv8w6NWHgRQxhSJHhJbhe+7yTZWamSLy59DD7lgx+TCpjrNIlIY8G8/aIbFrfe 9RAeZFob4+9gaLCu7Jq4Qnd6f/U/8gVqyOLnqCh2YKukGGr/zDVBsyyCrrN4BEtWc66A ZLF2kmnrjUtJWboyTAdIRbn8BKUlvUwbY7Nkat1SW3o4clTEKgsE2gpbwkMfltUpKVQ0 gwkcF4Jc96dOubSs7xgcVIEl6dGf6pKrH4t2WU2exjgMPiQXD11PAdcN+Te0e+Lkx5L+ P+8A== 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=0/fHOcsZ/l55nG++5wSq62hyeuwElgewgLEkUE/cyoo=; b=Z6Q+Ufr1Rwl+Z8I40vUoKpksQ2Neqd8D5JClpF0M9A68OfpSUoDaxigQWaiPS+zy9P YkxOWR4tcgrxzA+eWcer8tqafinhZDAeAdxPYOjTxpqVvASSv2oj1ouHQKCHz4Hvzs9/ DwEIgQm1f9sGwyzapVLZvojZMPSUzQV1aXctBwkDHQvzQDbuPXv9ylYw9Lfv1f5RQIpA HJoRKMbRHsEGx8MCnGqUsiDJidT3bcg8gTivFOrJen5rQygZUcKCmc2NBy+8xtCoqqSs NTkfTCA6lyS4HrNCsot2U1lKD/y3VajJ0BrnhfXL6I/M1xY1TulYN7meF4opPfhq8EkE r0VA== X-Gm-Message-State: AIkVDXKGh8jCpIsdvVPDKGEonHFGZe6k9WqnJxtCNtFPiFdTf/+VmU2Iz3Q0PDOkm4Ha34IMaWdZzzXkF8kc6g== X-Received: by 10.28.157.200 with SMTP id g191mr10260857wme.63.1484523039735; Sun, 15 Jan 2017 15:30:39 -0800 (PST) MIME-Version: 1.0 Received: by 10.194.34.197 with HTTP; Sun, 15 Jan 2017 15:30:19 -0800 (PST) In-Reply-To: References: Date: Mon, 16 Jan 2017 00:30:19 +0100 Message-ID: To: Wes Cc: PHP Internals Content-Type: multipart/alternative; boundary=001a114b41cc71c5eb05462a742f Subject: Re: [PHP-DEV] Unsetting declared fields From: ocramius@gmail.com (Marco Pivetta) --001a114b41cc71c5eb05462a742f Content-Type: text/plain; charset=UTF-8 Almost forgot: these examples apply also to private and protected properties. That's currently the only way to make the behavior consistent across friend objects (same scope). On Mon, Jan 16, 2017 at 12:27 AM, Marco Pivetta wrote: > 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 = 'baz'; > } > > class FooInterceptor extends Foo > { > private $wrapped; > public function __construct(Foo $wrapped) > { > $this->wrapped = $wrapped; > unset($this->bar); > } > public function __get(string $name) > { > var_dump('reading ' . $name); > return $this->wrapped->$name; > } > } > > $foo = new FooInterceptor(new Foo); > > var_dump($foo->bar); > > You can see a working example at https://3v4l.org/UtugD > > This behavior is protected from regressions since PHP 5.4, but has been > working since 5.0: https://github.com/php/php-src/blob/ > cd2b462a2742c79256668d4736644e34573c33d9/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 = 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 allowed >> for fields that are explicitly declared. I think unset() should set the >> 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 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? >> > > Marco Pivetta http://twitter.com/Ocramius http://ocramius.github.com/ --001a114b41cc71c5eb05462a742f--