Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:84560 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 68534 invoked from network); 11 Mar 2015 17:13:02 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 11 Mar 2015 17:13:02 -0000 Authentication-Results: pb1.pair.com header.from=rowan.collins@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=rowan.collins@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.212.174 as permitted sender) X-PHP-List-Original-Sender: rowan.collins@gmail.com X-Host-Fingerprint: 209.85.212.174 mail-wi0-f174.google.com Received: from [209.85.212.174] ([209.85.212.174:40455] helo=mail-wi0-f174.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 5D/EA-07702-C9770055 for ; Wed, 11 Mar 2015 12:13:01 -0500 Received: by wivr20 with SMTP id r20so40869901wiv.5 for ; Wed, 11 Mar 2015 10:12:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:content-type:content-transfer-encoding; bh=4INJfJMEwgWxNjg2a2Tc8ZXsYQM9Nx10yyxsVkO+g4M=; b=E7t4WwQ2cRcmyLUn6yZLign3y87uGVStkaaN116CWT+DX1z59oxYW3u6oS6neyZTq3 qnD9cLhHjMiH4opvbkpg+8D9pFPRZVe0QnRtPvx+00FjuepVSCua7sqbMEkFyR06tpwj HSy8xAziXqKyfBn50PfgXr3iyxF7ZNGyHavIPBUbllQrKnVZJtk7WmkuSCJ5Bed9ixIH ubUZLfJzOogWTH5EOB4/ecLYVPssWOGt8lx26goWbVsh3hLQGBHH1B9Z4ZA3mf5vurux eIJ3ktw9HgYRnEZCjpqsdEdyRJHvO+gbGNOgLtLKU4kW3GqEy6I8rpUGyohDndwOQflb 9m4A== X-Received: by 10.194.21.104 with SMTP id u8mr32861920wje.47.1426093977331; Wed, 11 Mar 2015 10:12:57 -0700 (PDT) Received: from [192.168.0.159] ([62.189.198.114]) by mx.google.com with ESMTPSA id ka1sm6298263wjc.2.2015.03.11.10.12.55 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 11 Mar 2015 10:12:56 -0700 (PDT) Message-ID: <55007777.3060106@gmail.com> Date: Wed, 11 Mar 2015 17:12:23 +0000 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.5.0 MIME-Version: 1.0 To: internals@lists.php.net References: <24.71.03288.592FDF45@pb1.pair.com> <54FE20E5.1060605@gmail.com> <55003D12.5010006@gmail.com> <55006B7E.1080500@gmail.com> <18.1A.07702.46170055@pb1.pair.com> In-Reply-To: <18.1A.07702.46170055@pb1.pair.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] Request Feedback for Instance Variable Sugar RFC From: rowan.collins@gmail.com (Rowan Collins) Johannes Ott wrote on 11/03/2015 16:46: > 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. True, but again, PHP doesn't give you a way of declaring that a variable *is* local, so it would be impossible in isolation to tell if $rate in this example is a property or just a temporary variable: public function setRatePercentage($rate_percent) { $rate = $rate / 100; $processed_amount = $amount * $rate; } >> 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. By "somewhere else", I meant "somewhere other than the local scope", so we're both saying the same thing here. >> 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. A lot of beginners start by reading other people's code, or get jobs working alongside experts. Having completely different styles of programming for experts and beginners harms collaboration. It is often observed that code is read much more often than it is written. >> 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. I agree, I was just demonstrating that we already have a keyword syntax, for scope resolution, as well as a well-behaved variable, $this. Regards, -- Rowan Collins [IMSoP]