New features worth looking forward to in PHP 7 (below)
This is the second article in the long-awaited PHP 7 series. Click here to read the first article, which is compiled by OneAPM engineers.
You may already know that PHP 7 will be released this year! Now, let's take a look at the new features and improvements of the new version.
In the first article of this series, we introduced some of the most important incompatibility fixes and two new features in PHP 7. In this article, we will learn about the other six functions of PHP 7.
Unicode code point escape syntax
Newly added escape characters ?? \ U, which allows us to specify the Unicode character code points (in hexadecimal format) in the PHP string ):
The syntax used here is\ U {CODEPOINT}. For example, this green heart ,?, It can be expressed as a PHP string _ "\ u {1F49A }"__.
Null merge operator
Another new operator ?? Null merge operator??Is actually a legend of the three-object operator. If it is notNull, Returns the left operand; otherwise, returns the right operand.
The point is that if the left operand is a non-existent variable, it will not be noticed. This is likeIsset (), Unlike? :Short three-object operator.
You can also link this operator to return the first non-null value of a given set.
$config = $config ?? $this->config ?? static::$defaultConfig;
Binding a closure on a call
Previously, Closure-> bindTo () and Closure: bind () added in PHP 5.4 allowed you to change $ this and the binding of the call range, either individually or separately, create a repeated closure.
Now, PHP 7 provides a simple way to reach the above features during the call, and binds $ this and the call range to the same object through Closure-> call. This method uses the object as the first parameter and then transmits it to other parameters in the closure, as follows:
class HelloWorld { private $greeting = "Hello";}$closure = function($whom) { echo $this->greeting . ' ' . $whom; }$obj = new HelloWorld();$closure->call($obj, 'World'); // Hello World
Group usage statement
If you have imported multiple classes from the same namespace and your IDE can automatically complete it, you will be very happy. For the sake of convenience, PHP 7 now has a group statement. This allows you to quickly and clearly specify multiple similar imports:
// Originaluse Framework\Component\SubComponent\ClassA;use Framework\Component\SubComponent\ClassB as ClassC;use Framework\Component\OtherComponent\ClassD;// With Group Useuse Framework\Component\{ SubComponent\ClassA, SubComponent\ClassB as ClassC, OtherComponent\ClassD};
You can also use it with use function and use const during constant import and function import. Hybrid import is also supported.
use Framework\Component\{ SubComponent\ClassA, function OtherComponent\someFunction, const OtherComponent\SOME_CONSTANT};
Generator improvement generator return expression
The generator has two new functions. First, the generator returns an expression that allows you to return a value when the generator (successful) is complete.
If you try to return any value before PHP 7, it will cause an error. However, now you can call$ Generator-> getReturn ()To obtain the return value.
If the generator has not returned, or an uncaptured exception is thrown, call$ Generator-> getReturn ()Will throw an exception.
If the generator is complete but no result is returned, null is returned.
Example:
function gen() { yield "Hello"; yield " "; yield "World!"; return "Goodbye Moon!";}$gen = gen();foreach ($gen as $value) { echo $value; }// Outputs "Hello" on iteration 1, " " on iterator 2, and "World!" on iteration 3echo $gen->getReturn(); // Goodbye Moon!
Generator delegate
The second feature is even more exciting: Generator Delegation. This allows you to return another iteratable structure, which can iterate itself ?? Array, iterator, or another generator.
It is important that sub-structure iterations are completed by the original loop of the outermost layer, just like a single plane structure rather than a recursive structure.
The same applies when sending data or exceptions to the generator. These data or exceptions are directly transmitted to the sub-structure, just as they are directly controlled by calls.
This is used Syntax yield, like this:
function hello() { yield "Hello"; yield " "; yield "World!"; yield from goodbye();}function goodbye() { yield "Goodbye"; yield " "; yield "Moon!";}$gen = hello();foreach ($gen as $value) { echo $value;}
In each iteration, the following output is displayed:
"Hello"
""
"World! "
"Goodbye"
""
"Moon! "
It is worth noting that, since sub-structures can generate their own keys, the same keys may be returned for multiple iterations ?? If this is important to you, you need to find a way to avoid it.
Internal exception
In PHP, fatal and captured fatal errors cannot be handled, or are difficult to handle. However, with internal exceptions, many such errors can be thrown.
Now, when a fatal or captured fatal error occurs, an exception is thrown, allowing you to handle it with ease. If you do not handle it, it will become a traditional fatal error such as uncaptured exceptions.
These exceptions are \ EngineException objects. Unlike all user exceptions, they do not inherit from the \ Exception class. This is to ensure that the code that captures the \ Exception class does not start to capture fatal errors in the future. To maintain backward compatibility.
In the future, if you want to capture both traditional and internal exceptions, you need to capture their new shared parent class, \ BaseException.
In addition, the parsing error in the eval () 'ed code will throw \ ParseException, while the type mismatch will throw a \ TypeException.
For example:
try { nonExistentFunction();} catch (\EngineException $e) { var_dump($e);}object(EngineException)#1 (7) { ["message":protected]=> string(32) "Call to undefined function nonExistantFunction()" ["string":"BaseException":private]=> string(0) "" ["code":protected]=> int(1) ["file":protected]=> string(17) "engine-exceptions.php" ["line":protected]=> int(1) ["trace":"BaseException":private]=> array(0) { } ["previous":"BaseException":private]=> NULL}
OneAPM for PHP can go deep into all PHP applications to complete application performance management. it can go deep into all PHP applications to complete application performance management and monitoring, including visibility of code-level performance problems, fast identification and tracing of performance bottlenecks, Real User Experience Monitoring, server monitoring, and end-to-end application performance management.
Coming soon!
It is only eight months away from PHP 7.0.0 release (there is not much time left for translators to translate). This version is probably the fastest performing version in PHP history. Although it only provides internal testing quality (RC5 can be downloaded now), PHP 7 is indeed expected.
In addition, you can help it become better.
Test your code
Use Rasmus's PHP 7 vagrant sandbox to start running your test suite or perform regular quality checks. Report errors to the project and retry regularly.
Help GOPHP7-EXT
One major obstacle to using PHP 7 is to ensure that all extensions are updated to make them compatible with the new Zend Engine 3.
If you use a small number of extensions, do you not receive enough attention from the maintainer ?? Or are you using your own extensions ?? Check the GoPHP7-ext project to make sure everything is ready after PHP 7 is released.
Document Writing
Each new function in PHP 7 has an RFC. You can find them on the PHP.net wiki and write new documents. You can write in the online GUI environment, including submitting (if you have karma) or submitting patches for review.
Summary
PHP 7 will be great!
PHP is one of the best languages in the world :)
Test your application. Help with migration expansion.
P.S. are you using PHP 7 now? How do you feel about the new feature? Are you not satisfied or dislike? When do you think you will upgrade? Let us know what you think!
Share your thoughts at the APM club!
OneAPM for PHP can go deep into all PHP applications to complete application performance management. it can go deep into all PHP applications to complete application performance management and monitoring, including visibility of code-level performance problems, fast identification and tracing of performance bottlenecks, Real User Experience Monitoring, server monitoring, and end-to-end application performance management.