Second, new functions and classes
Next, we'll see the new functions and classes in the plan:
1, Boolval ()
PHP has implemented the functions of Strval, Intval, and Floatval. In order to achieve consistency, the Boolval function is added. It can be calculated as a Boolean value or as a callback function.
2, HASH_PBKDF2 ()
PBKDF2 full name "password-based Key derivation Function 2", just like its name, is an algorithm that derives the encryption key from the password. This requires a cryptographic algorithm and can also be used to hash the password. For a more extensive description and usage example, see RFC.
3. Intl Extension
There will be many improvements to the intl extension. For example, there will be new Intlcalendar,intlgregoriancalendar,intltimezone,intlbreakiterator,intlrulebasedbreakiterator, Intlcodepointbreakiterator class. Before I actually didn't know there was so much about the intl extension, if you want to know more, I suggest you go to the latest announcements for calendar and Breakiterator.
4, Array_column ()
There is a proposal to add the Array_column (or Array_pluck) function, which behaves as follows:
$userNames = Array_column ($users, ' name ');
is the same as
$userNames = [];
foreach ($users as $user) {
$userNames [] = $user [' name '];
}
Get a column from the database like this, but the return is an array.
5. A simple password hashing API
Recent password leaks (from LinkedIn, etc.) show that even large web sites do not know how to hash passwords correctly. The use of Bcrypt has been advocated a few years ago, but most people still seem to be using a completely unsafe SHA1 hash value.
We think the reason for this may be that it is really hard to use the API of the crypt function. So we're going to adopt a new, simple API for secure password hashing:
$password = "Foo";
Creating the Hash
$hash = Password_hash ($password, Password_bcrypt);
Verifying a password
if (Password_verify ($password, $hash)) {
Password correct!
} else {
Password wrong!
}
The new hash API provides some more features, see RFC.
Third, language changes
What's really interesting now: new language features and enhancements.
1. Constant reference
Constant reference means that arrays can manipulate strings and array literals directly. Give two examples:
function randomhexstring ($length) {
$str = ";
for ($i = 0; $i < $length; + + $i) {
$str. = "0123456789abcdef" [Mt_rand (0, 15)]; Direct dereference of String
}
}
function Randombool () {
return [False, True][mt_rand (0, 1)]; Direct dereference of array
}
I don't think this feature will be used in practice, but it makes the language more consistent. See RFC.
2. Call the Empty () function (and other expressions) to work together
Currently, empty () language constructs can only be used in variables, not in other expressions. An error will be thrown in the specific code like empty ($this->getfriends ()). As a PHP5.5 this will become a valid code. For more information, see RFC.
? ? 3. Get the full category name
The functionality of the alias class and namespace short version of the namespace is introduced in PHP5.3. Although this does not apply to string class names:
Use Some\deeply\nested\namespace\foobar;
Does not work, because this would try to use the global ' FooBar ' class
$reflection = new Reflectionclass (' FooBar ');
To solve this problem, use the new Foobar::class syntax, which returns the full category name of the class:
Use Some\deeply\nested\namespace\foobar;
This works because foobar::class are resolved to "Some\\deeply\\nested\\namespace\\foobar"
$reflection = new Reflectionclass (foobar::class);
For more examples, see RFC.
4. Parameter jumping
If you have a function that accepts multiple optional parameters, there is currently no way to change only the last parameter, and all other parameters are the default values.
On the RFC example, if you have a function like this:
function Create_query ($where, er _by, $join _type= ", $execute = False, $report _errors = True) {...}
Then there is no way to set $report_errors=false, while the other two are the default values. In order to solve the problem of this jumping parameter is proposed:
Create_query ("Deleted=0", "name", default, Default, False);
I personally do not particularly like this proposal. In my eyes, the code needs this function, but it's not designed properly. A function should not have 12 optional parameters.
5. Scalar type hint
The scalar type hint was originally planned to enter 5.4, but was not done because of a lack of consensus. For more information on why scalar type hints are not being made into PHP, see: Scalar type hints are more difficult than you think.
For PHP5.5, the discussion of the scalar type prompt is another occurrence, which I think is a pretty good proposition.
It needs to specify the type by entering a value. For example: 123,123.0, "123" is a valid int parameter input, but "Hello World" is not. This is consistent with the behavior of the intrinsic function.
function foo (int $i) {...}
Foo (1); $i = 1
Foo (1.0); $i = 1
Foo ("1"); $i = 1
Foo ("1abc"); Not yet clear, maybe $i = 1 with notice
Foo (1.5); Not yet clear, maybe $i = 1 with notice
Foo ([]); Error
Foo ("abc"); Error
Getter and Setter
If you never like to write these getxyz () and SETXYZ ($value) methods, then this should be your most popular change. It is proposed to add a new syntax to define the setting/reading of a property:
Class TimePeriod {
Public $seconds;
Public $hours {
get {return $this->seconds/3600;}
set {$this->seconds = $value * 3600;}
}
}
$timePeriod = new TimePeriod;
$timePeriod->hours = 10;
Var_dump ($timePeriod->seconds); Int (36000)
Var_dump ($timePeriod->hours); Int (10)
There are, of course, more features, such as read-only properties. If you want to know more, see RFC.
6. Generator
Currently, custom iterators are seldom used because their implementation requires a lot of boilerplate code. The generator solves this problem and provides a simple boilerplate code to create iterators.
For example, you can define a range function as an iterator:
function *xrange ($start, $end, $step = 1) {
for ($i = $start; $i < $end; $i + = $step) {
Yield $i;
}
}
foreach (Xrange (Ten) as $i) {
// ...
}
The Xrange function above has the same behavior as the built-in function, but it is a little different: instead of returning all the values of an array, it returns the dynamically generated value of an iterator.
For a more in-depth introduction, see RFC.
7. List parsing and generator expressions
List parsing provides a simple way to perform small-scale operations on arrays:
$firstNames = [foreach ($users as $user) yield $user->firstname];
The above list resolution equals the following code:
$firstNames = [];
foreach ($users as $user) {
$firstNames [] = $user->firstname;
}
You can also filter arrays like this:
$underageUsers = [foreach ($users as $user) if ($user->age <) yield $user];
The generator expression is similar, but returns an iterator (for dynamically generated values) instead of an array.
For more examples, see the announcements in the mailing list.
Conclusion
As you can see, there are a lot of great new features that will be added to the PHP5.5. However, as I said, PHP5.5 is still in the early stages of development, so we are not sure whether these new features will be added.