Am 11.03.2015 um 18:12 schrieb 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; > } > As I said before I totaly agree for current state of PHP that with dynamic properties without declaration inside any scope we need a $this "operator" as mandatory. But in future if maybe we will have declaration, typesafety etc. I would vote then for a more java like behavior.
> >>> 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. > Yes I agree, but the beginners have to learn to read and understand and later on to write the expert code, that is no point for me to polute code with "unnecessary" snippets, because it will make it much harder to maintain then for everyone. And the best collaboration is to ask the experts to teach yourself. > It is often observed that code is read much more often than it is written. > > This argument really is a point for me. Because if you have lot of optional-keywords you have more to read and in worst case even to scroll horizontal. But I think we should come back to the topic of this thread, I would say that we should keep $this like it is for the moment, because of some other lecks in scope and variable concept of PHP. And I don't see the need of a shortcut-symbol for $this-> because that really makes the code much harder to read then the keyword "this" do. Regards -- DerOetzi -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php