PHP Wishlist
These are things that I think would help developers build better programs with far fewer lines of code. Simple enough? Let's get to it.
Scalar Type Hints and a Strict Mode
Currently one of the hottest RFCs in PHP history perhaps, Scalar Type Hints. This is a feature that allows developers to specify any type a given parameter should be. Currently PHP only supports object types and arrays for type hinting. This allows for string, integer, boolean, and the like.
However, nothing is simple in PHP. With this, some developers want conversion to happen if the wrong type is given (string to integer for example if '3' is passed in) while others want strict enforcing of types. The RFC has a middle-ground of loose typing by default like everything else in PHP, but the option to turn on a strict mode.
My issue with the RFC is... The syntax is horrible for declaring strict mode. I was actually entirely for it up until a few days ago. I read a comment on Reddit. This person describes a "strict" keyword that could be used on classes, their methods, or functions to declare the properties to be strictly checked. I much prefer this solution. I wouldn't go as far as to say a "weak" keyword too. Or even to define strict/weak at the individual property level. Simply say this is strict, or it isn't. Default to weak, strict is opt-in at the class, method, or function level as desired.
I believe at this time, the RFC should fail. Another should be made that adds scalar type hinting with standard type conversion if the wrong type is given. Then we can look over different syntax choices for adding strict type hinting and add that in later, even in a minor version of 7.x if it can't land before 7.0 drops.
I believe at this time, the RFC should fail. Another should be made that adds scalar type hinting with standard type conversion if the wrong type is given. Then we can look over different syntax choices for adding strict type hinting and add that in later, even in a minor version of 7.x if it can't land before 7.0 drops.
Property accessor syntax
Property accessor syntax makes a simpler way to define how a class property should be read and saved to. There is already a failed RFC that attempted to get this into PHP. From what I understand it was mostly rejected due to an implementation that internals didn't feel was clean/maintainable/whatever.
Currently there are two primary ways properties are manipulated. The first is to define a custom getter/setter for each property and set the logic in those. The second is to use magic methods to check for the property and set/get it. Having direct accessor syntax simply makes code cleaner and more legible.
Decimal number type
This is a much more difficult and long-reaching ask to be integrated. Currently PHP offers integer and float types. Integers store whole numbers only and float stores decimals, but not guaranteed complete accuracy. Also when compounding operations with floats, things can get worse. While bcmath functions are available, they use strings for parameters over a proper number type. Further, they are function calls instead of normal math syntax.
To fix this, I would like to see a decimal type added. One that is built on something that can accurately use a single number type to represent whole numbers and decimal places. Where we are able to use normal math operators to accurately calculate results, even in complex operations.
One implementation that could be used for a decimal type is DEC64. Created by Douglas Crockford and announced at HTML5 Dev Conf. This is a method aimed at fixing computer number storage so decimals are accurately represented. To be honest though, I have no clue how to really verify this and make sure it is good. It just sounds like a nice implementation that could be used.
Getting a new number type would be very involved to make sure everywhere data can come in has the option to be decimal. Further you wouldn't want to add it to loose typing structures either since that would cause legacy issues. Because of these issues (and more) it will probably never happen, but I can dream.
Conclusion
These are three major points that I see being helpful to everyday developers. With these we could build more well structured applications with significantly fewer lines of code.
Updates!
- Added to the end of my thoughts on scalar type hinting and how I think it would be best to proceed.