Re: Request Feedback for Instance Variable Sugar RFC
| From: | Johannes Ott | Date: | Wed, 11 Mar 2015 16:46:25 +0000 |
| Subject: | Re: Request Feedback for Instance Variable Sugar RFC | ||
| References: | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-84558@lists.php.net to get a copy of this message | ||
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