Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:84558 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 64012 invoked from network); 11 Mar 2015 16:46:33 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 11 Mar 2015 16:46:33 -0000 X-Host-Fingerprint: 91.115.245.126 91-115-245-126.adsl.highway.telekom.at Received: from [91.115.245.126] ([91.115.245.126:27508] helo=localhost.localdomain) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 18/1A-07702-46170055 for ; Wed, 11 Mar 2015 11:46:29 -0500 Message-ID: <18.1A.07702.46170055@pb1.pair.com> To: internals@lists.php.net Date: Wed, 11 Mar 2015 17:46:25 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.5.0 MIME-Version: 1.0 References: <24.71.03288.592FDF45@pb1.pair.com> <54FE20E5.1060605@gmail.com> <55003D12.5010006@gmail.com> <55006B7E.1080500@gmail.com> In-Reply-To: <55006B7E.1080500@gmail.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-Posted-By: 91.115.245.126 Subject: Re: [PHP-DEV] Request Feedback for Instance Variable Sugar RFC From: mail@deroetzi.de (Johannes Ott) Am 11.03.2015 um 17:21 schrieb Rowan Collins: > > My reasoning is that code that is ambiguous is hard to read. If "$foo" > can mean either "a local variable called $foo" or "a property of the > current object called $foo", then you have to know which it is in order > to understand what code is doing. So for clean code rules you should do smaller methods if you can't even see clearly whether you declared $foo locally or not. > > This is not about "a strict OOP-world", incidentally, it's about scoping > rules. Java imports object properties into the scope of each method, PHP > does not. Even if properties had to be declared (which is probably a > good idea), local variables still wouldn't be - in Java, the fact that > it's not declared locally means it *must* be coming from somewhere else. > No if it is not declared locally it doesn't mean it's coming from somewhere else, but it means it is coming from one of the next higher scopes which is normally the Object. > I also know that when I was first learning Java at school, it confused > me immensely which variables I was allowed to access in static contexts. > In PHP, that's simple - if you can't access $this, you can't access any > of it's properties. > I'm not talking about beginners code, but about professional clean code. If you're doing as a beginner you can still use the $this keyword to make it clearer code for you to understand. But if you want to do huge applications you should have understand the difference between static and non-static context. And if it would be defined well in PHP, modern IDEs should be able to help you to do no mistakes. > > This is true of any object variable. > > These resolve the scope of the property/method to the variable $foo: > > $a = $foo->a; > $foo->some_function(...); > > These resolve the scope of the property/method to the variable $this: > > $a = $this->a; > $this->some_function(...); > Okay I agree with this point. > If you want to resolve the scope of a method to the current object > without using the variable $this, you can also use the "static" keyword; > these are equivalent: > > $this->some_function(...); > static::some_function(...); > // http://3v4l.org/E0XYs I don't care whether I use $this-> or static:: as keyword. In this case I would even prefer $this-> because static in instance context is in my opinion really confusing. > > There is nothing unvariable-like about $this, so if variables begin with > $, $this should begin with $. > > Regards, Regards -- DerOetzi