This is the second of our long-awaited PHP 7 series article. Click here to read the first article by the OneAPM engineer compiled and collated.
Maybe you already know that the release of the play PHP 7 will come this year! Now, let's look at what new features and improvements are in the new version.
In the first part of this series, we introduced some of the most important incompatibilities in PHP 7, as well as two more features. In this article, we'll look at another six big features of PHP 7.
Unicode code point Escape syntax
The newly added escape character?? \u, allows us to explicitly specify the Unicode character code point (in hexadecimal) within the PHP string:
The syntax used here is \u{codepoint} . For example this green heart shape,?, can be expressed as PHP string __ "\u{1f49a}" __.
Null merge operator
Another new operator?? Null merge operator ?? , is actually the legendary three-mesh operator. If it is not Null , the left operand is returned, otherwise the right operand is returned.
The point is that if the left operand is a non-existent variable, it does not attract attention. This is like isset () , not like ?: short three-mesh operator.
You can also link the operator to return the first non-null value for a given collection.
$config = $config?? $this->config?? Static:: $defaultConfig;
Binding closure of a call
Previously, the addition of Closure->bindto () and Closure::bind () in PHP 5.4 allowed you to change the bindings for $this and call scopes, and create a repeating closure at the same time or individually.
Now, PHP 7 adds an easy way to achieve the above functionality at invocation, binding $this and call scopes to the same object through Closure->call (). The method takes the object as the first parameter and then the other parameters that are passed to 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 claims
If you've ever imported more than one class from the same namespace, and your IDE can do it automatically, you're sure to be happy. For others, for simplicity, PHP 7 now has a group usage 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 a usage function, using const when a constant import is associated with a function import. Hybrid import is also supported.
Use framework\component\{ Subcomponent\classa, function othercomponent\somefunction, const Othercomponent\some_constant};
Builder improvements
The generator returns an expression
The generator has two more features. The first is the generator return expression, which allows you to return a value when the generator (succeeds) completes.
Before PHP 7, if you try to return any value will result in an error. However, you can now call $generator->getreturn () to get the return value.
If the generator has not returned, or throws an uncaught exception, the call $generator->getreturn () throws an exception.
Returns NULL if the generator has completed but has not returned.
Examples are as follows:
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!
Builder delegate
The second feature is more exciting: the builder delegate. This allows you to return to another iterative structure, which iterates over itself?? Whether it's an array, an iterator, or another generator.
It is important that the iteration of the substructure is done by the outermost primitive loop, like a single planar structure rather than a recursive structure.
This is also the same as when sending data or exceptions to the generator. These data or exceptions are passed directly to the substructure, as if directly controlled by the call.
This is the yield that uses the syntax, 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 output is:
"Hello"
" "
"World!"
"Goodbye"
" "
"Moon!"
It is worth mentioning that because a child structure can produce its own key, multiple iterations can completely return the same key?? If this is important to you, you need to try to avoid it yourself.
Inner exception
In PHP, fatal and catch fatal errors are never handled or difficult to handle. But with an internal exception, many of these errors can now throw exceptions.
Now, when a fatal or catch fatal error occurs, an exception is thrown that allows you to handle it calmly. If you do not handle it, it will become a traditional fatal error such as an uncaught exception.
These exceptions are \engineexception objects. They are not like all user exceptions and do not inherit from the \exception class. This is to ensure that the code that captures the \exception class now does not start capturing 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, parsing errors in the eval () ' Ed code will throw \parseexception, and a type mismatch will throw a \typeexception.
The following example:
try { nonexistentfunction ();} catch (\engineexception $e) { var_dump ($e);} Object (engineexception) #1 (7) { ["message":p rotected]=> string (+) "Call to undefined function Nonexistantfunction () " [" string ":" Baseexception ":p rivate]=> string (0)" " [" Code ":p rotected]=> Int (1) ["File":p rotected]=> string "engine-exceptions.php" ["line":p rotected]=> Int (1) [" Trace ":" Baseexception ":p rivate]=> Array (0) { } [" Previous ":" Baseexception ":p rivate]=> NULL}
OneAPM for PHP is able to drill down into all of the PHP applications to perform application performance management in-depth application performance management and monitoring within all PHP applications, including code-level visibility, rapid identification and traceability of performance bottlenecks, real user experience monitoring, Server monitoring and end-to-end application performance management.
Coming soon!
Only eight months after the release of PHP 7.0.0 (the translator has little time left), this version is probably the fastest-performing version of PHP history. Although now it only has internal testing quality (currently RC5 is available for download), PHP 7 does look good.
And you can help it get better.
Test your code
Use Rasmus's PHP 7 vagrant sandbox to start running your test suite, or perform a regular quality check. Reports errors to the project and retries periodically.
Help Gophp7-ext
A major hurdle to using PHP 7 is making sure that all extensions are updated to be compatible with the new Zend Engine 3.
If you use a smaller audience, do not get enough attention from their maintainers?? Or do you use your own extensions?? Check out the Gophp7-ext project to make sure everything is ready after PHP 7 is released.
Writing documents
Each new feature in PHP 7 has an RFC. You can find them on php.net wiki and write new documents on this basis. You can write in an online GUI environment, including submitting (if you have Karma) or submitting patches for review.
Summarize
PHP 7 will be great!
PHP is the best language in the world, not one of them:)
Test your application in a hurry. Help migrate the extension.
P.S. Are you already using PHP 7? How do you feel about new features? Are there any places that you are not satisfied with, or that you do not like? When do you think you will upgrade? Let us know what you think!
Share your thoughts and make the most of the APM club!
OneAPM for PHP is able to drill down into all of the PHP applications to perform application performance management in-depth application performance management and monitoring within all PHP applications, including code-level visibility, rapid identification and traceability of performance bottlenecks, real user experience monitoring, Server monitoring and end-to-end application performance management.