-
-
Save ShawnMcCool/4253430ceaebb9bce815 to your computer and use it in GitHub Desktop.
====== PHP RFC: Instance Reference Sugar ====== | |
* Version: 0.2 | |
* Date: 2015-03-09 | |
* Author: Shawn McCool, [email protected] | |
* Status: In Discussion | |
===== Summary ===== | |
In order to access instance variables and methods, one must use the `$this->` prefix. | |
The problem with this is that it reduces expressiveness in the language and increases the amount of unnecessary decoration, reducing readability. | |
This RFC proposes a single character syntax sugar form of `$this->`. Instead, an `@` can be used to reference instance variables and methods. | |
The @ replaces the normal $ variable prefix. | |
===== Example ===== | |
<file php Addition.php> | |
<?php | |
class Addition { | |
private $number | |
public function __construct($number) { | |
@number = $number; | |
} | |
public function original() { | |
return @number; | |
} | |
public function addTo($amount) { | |
return @add(@number, $amount); | |
} | |
private function add($one, $two) { | |
return $one + $two; | |
} | |
} | |
</file> | |
===== Implementation ===== | |
@ is basically a macro that expands to `$this->` | |
===== Backwards Compatibility ===== | |
Leave `$this->` available. | |
@ is currently used for error suppression. This error suppression format should be removed in favor of error exceptions. | |
===== Proposed PHP Version(s) ===== | |
This is proposed for the next PHP x, currently PHP 7. |
I'm not arguing in general about the heavy amount of decoration used in PHP. I just don't consider $this->
to be decoration or heavy. Other areas: function () use ($bar) {}
, sure. Or even protected static function blah()
... I just don't consider this one to be a) bad or b) heavy.
As far as direct field access:
class MyClass {
private $var;
public function getVar() {
return $var;
}
}
I really don't like that syntax. In trivial examples, it's great. In complex examples (more than 100 loc) it can become really hard to track down. It requires you to either use an IDE or read through hundreds of lines of code to figure out (since you need to read the entire method body, as well as class definitions of the current class as well as any parent). That's why Java uses the _
prefix convention for member variables. So that it's easier to at a glance tell
And I'm strongly opposed to any feature that requires the use of an IDE to reasonably use.
But thanks for sharing the idea :-)
Is there any news on this? ;)
Would really like to see this implemented at some point.
Not sure I have any preference on the character used, just something shorter than $this->
xD
What I definitely don't expect from PHP.
Of course I agree with '@' not being an option. I actually laughed for like 20 seconds straight when I added the line to backwards compatibility about removing error suppression.
I agree that
this
reference is important. But, I disagree that such a change as this wouldn't increase readability.I'm not particularly interested in Ruby. However, the nice thing about Ruby (relevant to this conversation) is that it conforms to the uniform access principle. I agree that this probably isn't the right choice for PHP.
What I'm looking for.
Ideally, I'd like to see PHP scoping modified to support direct field access from methods.
This requires
$this->
for referencing fields when names exist in the function.I realize that I'm just not going to get that. I figured I might throw a proposal for a macro on the table.
I always enjoy using languages that allow access to fields in this way. There's really NOT a need for _ prefix because you do have the ability to reference
this
.So, constructors can still be crazy as normal:
But, actual methods that have the behavior can be cleaner and more concise. One thing that I find sub-optimal in PHP is the heavy amount of decoration used.
I do know that we won't agree on what makes nicer to use / read syntax. But, I very much appreciate you spending your time talking to me about this.
Thanks, Anthony.