Skip to content

Instantly share code, notes, and snippets.

@ShawnMcCool
Last active August 29, 2015 14:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ShawnMcCool/4253430ceaebb9bce815 to your computer and use it in GitHub Desktop.
Save ShawnMcCool/4253430ceaebb9bce815 to your computer and use it in GitHub Desktop.
PHP RFC: Instance Reference Sugar 0.2
====== PHP RFC: Instance Reference Sugar ======
* Version: 0.2
* Date: 2015-03-09
* Author: Shawn McCool, shawn@heybigname.com
* 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.
@ircmaxell
Copy link

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 :-)

@martindilling
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment