Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:92959 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 7975 invoked from network); 29 Apr 2016 19:32:24 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 29 Apr 2016 19:32:24 -0000 Authentication-Results: pb1.pair.com header.from=nikita.ppv@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=nikita.ppv@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.161.175 as permitted sender) X-PHP-List-Original-Sender: nikita.ppv@gmail.com X-Host-Fingerprint: 209.85.161.175 mail-yw0-f175.google.com Received: from [209.85.161.175] ([209.85.161.175:33727] helo=mail-yw0-f175.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 68/34-26386-4C6B3275 for ; Fri, 29 Apr 2016 15:32:21 -0400 Received: by mail-yw0-f175.google.com with SMTP id t10so207503052ywa.0 for ; Fri, 29 Apr 2016 12:32:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc; bh=hwUkUcy03JQ2/iGGU+mvpdvHXVpmaGjzwuDc/CYGzo4=; b=SvZIcOkoqeSr6Al+CeyLLDm93UGFCIe4/cr/t5Kvy6SKEfYzAhq7URa3QzYXaoH/uF cM5fK11CaN/W+scWCZd5FAX4VRRvuBdK8eO+kmFU1vlsaVuxJEnDbvVPa377HHq9gO53 n/9IJ5hxL1Ati71XJA2TJH4Zd2iA17Za5Kqgkf6qQrreRDw8ikg64PQpX7b77961PdK/ QBvgqIO/3Q20A8SLaK0sVyTjb48KAU1L81z1Hq2jY4Cyc6rsmZUQPe7IiTjB6BgiLNiD UvEo8dM9R4hRWtYWpn+GyBOrqXbKwufs4eHwSmELp95UE8W5qwAXsm2OIr1/GNYg9TMJ dX8w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:to:cc; bh=hwUkUcy03JQ2/iGGU+mvpdvHXVpmaGjzwuDc/CYGzo4=; b=Z26j5+j9t0BQLVyVdkgaNLEcaio8sihkhHZq7Rdt4mabTLQHkvXRduyR5YWtDubp1/ fHKqSn+Zm1K1v4uTw5v39Uc/avJS/70wuDqHg2C12iRCDb1V85q45oiTB+WiCdYilaJC 5pUeAYYKp7O0XFUfzyyahhXb+A0kD5abZ7qTvza6kTsJ18rNQZ+sWfNvU0XSnXjuohC6 rMuHGfDXRf/xonED7vYL0oUKqXABrzNx3xn6l3PTXKFOUpGrFBy0oBuVyX2L9sflFJR/ hIqB9kLCPW/R5eSGNIFU4JxToYcfZuBQiz/yQqmE1ULid07c4PXj09i1lKBisPKFX/oQ Acsw== X-Gm-Message-State: AOPr4FUsyXsRaqygZUsaFXwriuVf9PtnnpGMW349IRaUyBaOb8iJXVcKjr42WetGn/f6XDlzeI9rjofRCmrs+w== MIME-Version: 1.0 X-Received: by 10.129.3.17 with SMTP id 17mr14117403ywd.288.1461958337715; Fri, 29 Apr 2016 12:32:17 -0700 (PDT) Received: by 10.13.239.3 with HTTP; Fri, 29 Apr 2016 12:32:17 -0700 (PDT) In-Reply-To: <3de7a02a-0d76-cd48-6001-b1b9456c2690@gmail.com> References: <3de7a02a-0d76-cd48-6001-b1b9456c2690@gmail.com> Date: Fri, 29 Apr 2016 21:32:17 +0200 Message-ID: To: Stanislav Malyshev Cc: PHP internals Content-Type: multipart/alternative; boundary=001a1142d7be6585260531a4b355 Subject: Re: [PHP-DEV] [RFC] Forbid $this being null in instance methods From: nikita.ppv@gmail.com (Nikita Popov) --001a1142d7be6585260531a4b355 Content-Type: text/plain; charset=UTF-8 On Fri, Apr 29, 2016 at 9:10 PM, Stanislav Malyshev wrote: > Hi! > > > I'd like to submit a small RFC for your consideration, which ensures > > something that really ought to be a given already: If you use $this in an > > instance method, you should actually get a $this (and not NULL). > > > > https://wiki.php.net/rfc/forbid_null_this_in_methods > > > > As this is targeting PHP 7.1, the RFC is careful to retain compatibility > > with certain legacy PHP 4 code patterns. > > Unfortunately, this does not exactly preserve compatibility. Consider: > > public function legacyMethod() { > if(is_null($this)) { > trigger_error(E_DEPRECATED, "legacyMethod() should be called with an > object now!"); > return self::getDefaultInstance()->legacyMethod(); > } > print "I am an ugly legacy method!"; > $this->doSomeStuff(); > return true; > } > > Unfortunately, I've seen this pattern not once when dealing with APIs > which were grown from PHP 4 code and must maintain BC - e.g. have code > relying on legacyMethod() both being called statically and called on an > object and legacyMethod() being overridden by extending classes. It is > an ugly code and an ugly situation to be in, but it is a real pattern > that happens. > > Despite the RFC claiming HHVM does not support this pattern, it does: > https://3v4l.org/1QrY7 > Unlike PHP, it doesn't even throw deprecated error in that case. > Wow, thanks for pointing this out! I'm really stumped as to why I thought HHVM doesn't support it. I've dropped the remark now. > Given that this RFC does not seem to enable anyone to do anything > additional that couldn't be done before, just removes the features from > language, I see any advantage in doing it so far. > > I also do not think enabling using $this as a regular variable is a > particularly good idea. It does not add anything - there's no real need > to name your variables specifically "$this", you can choose any other > name - but it creates potential both for confusion and for weird errors > when you refactor stuff. Again, don't see any real advantage in this. > The thing is that currently we allow using $this as a normal variable in some ways (accepted as a function argument for example), but then don't allow it in others (accessing properties on it). However, I agree that this is probably the wrong direction to go here, instead we should make sure that $this cannot be used at all. I will consider this. Thanks, Nikita --001a1142d7be6585260531a4b355--