I've never submitted an RFC. Whether or not you're interested in the
feature, please consider giving me feedback on the RFC itself so that I can
better understand how to succeed in the process.
====== PHP RFC: Instance Variable Sugar ======
- Version: 0.1
- Date: 2015-03-09
- Author: Shawn McCool, shawn@heybigname.com
- Status: In Discussion
===== Summary =====
In order to access instance variables, 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.
The @ replaces the normal $ variable prefix.
===== Example =====
<file php MyClass.php> <?php class Addition { private $numberpublic function __construct($number) {
@number = $number;
}
public function original() {
return @number;
}
public function addTo($amount) {
return @number + $amount;
}
}
</file>
===== Backwards Compatibility =====
Leave $this->
available.
===== Proposed PHP Version(s) =====
This is proposed for the next PHP x, currently PHP 7.
--
Shawn McCool | Big Name
shawn@heybigname.com
heybigname.com
Hey Shane,
The @ symbol in PHP is currently an error control operator1. Does that
mean you will be reassigning the error control operator to another
character?
I've never submitted an RFC. Whether or not you're interested in the
feature, please consider giving me feedback on the RFC itself so that I can
better understand how to succeed in the process.====== PHP RFC: Instance Variable Sugar ======
- Version: 0.1
- Date: 2015-03-09
- Author: Shawn McCool, shawn@heybigname.com javascript:;
- Status: In Discussion
===== Summary =====
In order to access instance variables, 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.The @ replaces the normal $ variable prefix.
===== Example =====
<file php MyClass.php> <?php class Addition { private $numberpublic function __construct($number) {
@number = $number;
}public function original() {
return @number;
}public function addTo($amount) {
return @number + $amount;
}
}
</file>===== Backwards Compatibility =====
Leave
$this->
available.===== Proposed PHP Version(s) =====
This is proposed for the next PHP x, currently PHP 7.
--
Shawn McCool | Big Name
shawn@heybigname.com javascript:;
heybigname.com
Hi,
I've never submitted an RFC. Whether or not you're interested in the
feature, please consider giving me feedback on the RFC itself so that I can
better understand how to succeed in the process.====== PHP RFC: Instance Variable Sugar ======
- Version: 0.1
- Date: 2015-03-09
- Author: Shawn McCool, shawn@heybigname.com
- Status: In Discussion
===== Summary =====
In order to access instance variables, 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 might decrease readability, since we already comfortable with the
syntax $this->something, in my opinion
This RFC proposes a single character syntax sugar form of
$this->
.
Instead, an@
can be used to reference instance variables.The @ replaces the normal $ variable prefix.
===== Example =====
<file php MyClass.php> <?php class Addition { private $numberpublic function __construct($number) {
@number = $number;
}public function original() {
return @number;
}public function addTo($amount) {
return @number + $amount;
this is a BC break. this is the same as constant number + $amount. so this
syntax is not feasible.
}
}
</file>===== Backwards Compatibility =====
Leave
$this->
available.===== Proposed PHP Version(s) =====
This is proposed for the next PHP x, currently PHP 7.
--
Shawn McCool | Big Name
shawn@heybigname.com
heybigname.com
--
Reeze Xia
http://reeze.cn
Hi Shawn,
My opinion is that even though the "@" operator should be deprecated in
further along the line removed, it should not be repurposed for anything,
it has too much legacy imho.
While a shortcut might be a good idea, I personally favour the $this->var
syntax just for muscle memory if nothing else.
Stelian
Hi,
I've never submitted an RFC. Whether or not you're interested in the
feature, please consider giving me feedback on the RFC itself so that I
can
better understand how to succeed in the process.====== PHP RFC: Instance Variable Sugar ======
- Version: 0.1
- Date: 2015-03-09
- Author: Shawn McCool, shawn@heybigname.com
- Status: In Discussion
===== Summary =====
In order to access instance variables, 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 might decrease readability, since we already comfortable with the
syntax $this->something, in my opinionThis RFC proposes a single character syntax sugar form of
$this->
.
Instead, an@
can be used to reference instance variables.The @ replaces the normal $ variable prefix.
===== Example =====
<file php MyClass.php> <?php class Addition { private $numberpublic function __construct($number) {
@number = $number;
}public function original() {
return @number;
}public function addTo($amount) {
return @number + $amount;this is a BC break. this is the same as constant number + $amount. so this
syntax is not feasible.}
}
</file>===== Backwards Compatibility =====
Leave
$this->
available.===== Proposed PHP Version(s) =====
This is proposed for the next PHP x, currently PHP 7.
--
Shawn McCool | Big Name
shawn@heybigname.com
heybigname.com--
Reeze Xia
http://reeze.cn
In order to access instance variables, 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.
I disagree with this premise. The $this here is not just decorative syntax, it's an actual variable, which can be passed around just like any other variable. The -> is then the standard way of accessing a property or method, applied to that variable.
This is beautifully simple, and expresses what's going on perfectly: if you look at $this->foo, and ask "which foo is being referenced here?", the answer is clearly "the one which is a property of $this".
The only places I can see value in a shorthand are in constructors and setters, where you're just copying from one name to another ($this->foo = $foo). But the part that's redundant there is the variable/property names, not the $this, so what you need is getter/setter support, or shorthand constructors (you should be able to find previous RFCs for both).
Regards,
Rowan Collins
[IMSoP]
Hi!
The problem with this is that it reduces expressiveness in the
language and increases the amount of unnecessary decoration,
reducing readability.I disagree with this premise. The $this here is not just decorative
syntax, it's an actual variable, which can be passed around just like
any other variable. The -> is then the standard way of accessing a
property or method, applied to that variable.
Completely agree. The problem with going to far with overly "expressive"
code - i.e. one that uses as little characters as possible - is that it
becomes less and less comprehensible. $this-> is not too long to be any
real problem (and if it's really that troublesome one always can code a
macro in their editor). It is abundantly clear to both novice and
seasoned programmer. It is a subclass of wider standard syntax (-> as
member access). It has shared pattern for properties and methods, so you
don't need to keep too many concepts in your mind to parse it. I don't
think saving half-dozen characters is worth sacrificing all that.
Stas Malyshev
smalyshev@gmail.com
In order to access instance variables, 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.
I disagree with this premise. The $this here is not just decorative syntax, it's an actual variable, which can be passed around just like any other variable. The -> is then the standard way of accessing a property or method, applied to that variable.This is beautifully simple, and expresses what's going on perfectly: if you look at $this->foo, and ask "which foo is being referenced here?", the answer is clearly "the one which is a property of $this".
The only places I can see value in a shorthand are in constructors and setters, where you're just copying from one name to another ($this->foo = $foo). But the part that's redundant there is the variable/property names, not the $this, so what you need is getter/setter support, or shorthand constructors (you should be able to find previous RFCs for both).
Regards,
I concur. I don't see much advantage of a short hand in this case, but
I would love to see someone pick up both the property RFC (which implied
short-hand getter/setters) and shorthand constructors. (It's undoubtedly
too late for 7.0, but they'd be great additions for 7.1.)
--Larry Garfield