Hi everyone

There was an RFC about a C#-like property syntax almost four years ago that got 
declined:
https://wiki.php.net/rfc/propertygetsetsyntax-v1.2

It failed by only 2-3 votes. In my opinion, PHP is still in need of this 
feature as it would make the language much more expressive. Let me start by 
defining the problem. First of all, almost all PHP frameworks hide their 
variables behind getters and setters. Here are some of the reasons why that is 
a good idea, I’m sure there are many more:

- Ability to sanitize input
- Ability to lazily load data when requested (getter was called)
- Ability to expose only getter or setter publicly
- Easily mockable accessors for unit tests
- Ability to execute arbitrary code when accessors are called
- Modifiable business logic without breaking the API

Here are some of the negatives, though there are probably more here as well:

- Large boilerplate for normal getters/setters
- Inconsistent syntax for reading/writing data
    - Calling a method for setting data instead of using the more intuitive `=` 
operator
    - Calling a method for accessing foreign variables while accessing private 
variables directly
- Inability to use built in assignment operators like .=, +=, -=, etc.
- Disregard of the DRY principle

The positives clearly outweigh the negatives as they are mostly syntactical. 
This is probably why using getters/setters everywhere has been a good-enough 
solution for such a long time. Because of that, just like the positives, the 
negatives have become a part of the language. Clearly, abandoning getters and 
setters is no option for anyone. Nonetheless, the RFC I linked above offers a 
way to get rid of the negatives without sacrificing any of the positives.

Are there still any people who would like to see this happening just as much as 
I do?
I’m wondering if some of the people who have voted against the RFC might have 
changed their opinion or vice versa.

Many thanks for reading!

Regards,
Ilija

Reply via email to