New in PHP 7
Let’s review the new constants, functions, classes, interfaces and trait that PHP 7 features. We have reviewed the core of PHP (as of June 1rst) and spotted quite some new structures that are already available to you if you’re testing the water with PHP7.
New Constants in PHP 7
In the core, there will only be this one : PHP_INT_MIN. It represents the smallest integer available. On 64 bits platform, it will be -9223372036854775808, and on 32 bits, it will be -2147483648. This is useful to every code that needs to build the smallest integer from the largest one.
New functions on PHP 7
preg_replace_callback_array
This is a new feature of PHP 7, that will probably rejoice people who are missing the /e option of PHP 5.4 and less. There is a whole RFC from Wei Dai(https://wiki.php.net/rfc/preg_replace_callback_array) about this.
The idea is to be able to run several regex on one piece of string, and associate each of the regex with a different callback, thanks to the hashmap notation:
<?php $string = 'abcde'; $d = new d(); $code = preg_replace_callback_array( array( "^a^" => function($matches) {return "A";}, // callback is a closure "#b#is" => 'callback', // callback is a function "#c#is" => array('c', 'callback'), // callback is a static method "#D#is" => array($d, 'callback'), // callback is a method ), $string); print $code; // classes C and D were omitted in the example ?>
get_resources
This list all the existing resources. Resources are available for files, xml parser, directories, HTTP servers or some databases connexions. If you lost track of some of them, you will find them here.
<?php print_r(get_resources()) ; $dir = opendir('.') ; print_r(get_resources()) ; ?>
This will display :
Array ( [1] => Resource id #1 [2] => Resource id #2 [3] => Resource id #3 ) Array ( [1] => Resource id #1 [2] => Resource id #2 [3] => Resource id #3 [4] => Resource id #4 [5] => Resource id #5 )
As you note, opendir() creates two resources. To understand why, we need to use the old-timer function ‘get_resource_type‘ to learn that a stream and a stream-context were created by opendir.
random_bytes and random_int
Both are coming from another RFC : the need for CSPRNG. It is a cryptographically secure pseudo-random number generator. Currently, we rely on rand() or, better, on mt_rand. They are directly inherited from C language, which is also looking forward to change this, because of security concerns (http://en.wikipedia.org/wiki/Random_number_generator_attack)
Random_bytes generates random bytes (sic), as long as you requested. Random_int generates an integer, whose value is between the two arguments provided.
<?php print random_bytes(10); print random_bytes(0, 100); ?>
chroot
I found out that chroot is running a strange life It is a core function since PHP 4, and has always been available until PHP7, as says the manual (and so I believed). Except, that all my local installations (5.2 to 5.6) are missing it. I also checked on the very useful 3v4l.org site, to find them unavailable on 5.4.* and 5.5.*, but not in more recent or older versions. If you’re relying on this feature, check twice !
intdiv
This function return the integer division of the first argument by the second. Integer division is the same than decimal division, except that numbers are always integers. As such, there will be a remainder (when this remainder is 0, we usually ignore it), which is accessible via the % operator. Up to now, one would require decimal division and some casting to get this value.
<?php print (123 / 45) . "\n"; print (intdiv(123, 45)) ."\n"; print (123 % 45) ."\n"; // ($a / $b) * $b + ($a % $b) == $a
//In PHP7, this will always be true ?>
error_clear_last
Cleaning function in the ‘error’ family. error_get_last() returns the last error, and error_clear_last removes it.
<?php $a = $b; // $b doesn't exists, so an error is emitted var_dump(error_get_last()); // Error about non-existing $b error_clear_last(); var_dump(error_get_last()); // No more error message ?>
Removed functions
They don’t need any presentation, since you already know about them if you’re using them. You won’t need to know about them if they get removed before you use then (don’t!). Some extensions may remove some functions too, though I haven’t checked thoroughly yet.
- ereg
- ereg_replace
- eregi
- eregi_replace
- split
- spliti
- sql_regcase
Classes
The new classes :
- BaseException
- EngineException
- ParseException
- TypeException
- ClosedGeneratorException
- ReflectionGenerator
- AssertionException
BaseException
The three first of the new one are linked to the new Exception system, that introduces BaseException, as a replacement for Exception at the root of all exceptions.
TypeException should be thrown when an argument is of the wrong time.
ClosedGeneratorException signals situations where the generator has run out of values and is finalized. As such, requesting yet a new value will trigger this exception.
ReflectionGenerator will help when generating traces with generators : it is possible to follow the usage of a generator, and be able to generate a trace while executing the generator.
AssertionException are the exception to be used with the assert functions.
Note that some keywords were reserved for PHP so they could be used in Scalar TypeHinting (or not), but they didn’t generate any class.
Traits and interfaces
Finally, no new traits and interfaces were introduced or removed. A ‘throwable‘ interface is under discussion but not yet available in the code.
PHP modernization
PHP 7 is definitely looking at speeding up the code and cleaning the old C code. There are some new features, but reasonably few. This will be good for transition, and we may expect a bloom of new functionalities later, when the code has stabilized.
Also, keep in mind that PHP 7 is still under writing : we will most probably see some new features until it is actually finalized. We’ll review this document as we draw closer to the deadline.
And since most of them are not in the documentation yet, it it good time to document this.