This is the first article in our long-awaited PHP 7 series.
Perhaps you already know, I in PHP 5.0.0 time axis of the RFC (Request for Comments) passed, PHP 70% is the name of the next major version of PHP.
No matter how you feel about this topic, PHP 7 is a big event and it will be released this year! After the RFC for PHP 7.0 timeline almost unanimously passed (32 to 2), all features are now established and we will see the first candidate version (RC) released in mid-June.
But what does that mean to you? When we see a new version of 5.x released, many Web hosts are reluctant to upgrade. Does an important new release not bring a huge backward compatibility partition, making the upgrade slower?
The answer is: depending on the situation. Please read on.
In the new version, many language boundary conditions have been processed. In addition, the performance and inconsistency repair is also a key concern of this edition.
The following is a detailed discussion.
Non-compatibility fixes
Unfortunately, the needle/haystack problem has not been fixed. However, two important RFCs have been passed, and they will bring some long-awaited internal consistency with the user layer.
The biggest (and most imperceptible) change is the new abstract syntax tree (AST)-the middle representation of the code during compilation. With this representation, we can clean up some inconsistencies in the edge situation and prepare for some great tools for the future, such as using the AST to generate better-performing OpCode.
Second, the introduction of uniform variable syntax can lead to more problems. This resolves many incompatibility issues in expression evaluation. For example, you can use ($object->closureproperty) () to call a closure function that is assigned to a property, and to perform a chain static call, as follows:
class Foo {static $bar = ' Baz ';} Class Baz {static $bat = ' Hello world ';} Baz:: $bat = function () {echo "Hello World";}; $foo = ' foo '; ($foo:: $bar:: $bat) ();
However, some of the syntax is also changing. In particular, use variable->variables/properties syntax.
Prior to PHP 7, $obj-$properties [' name '] would access the property whose name belongs to the $properties array name key (name key). After you use the universal variable syntax (Universal Variable Syntax), it accesses the name key of the property whose name belongs to "$properties".
Or, more succinctly, if you use the following syntax:
$obj $properties [' name ']
In PHP 5.6, it will be parsed as:
$obj->{$properties [' Name ']}
In PHP 7, it is:
{$obj-$properties} [' Name ']
Variable->variables are usually used in border situations, and according to my experience, variable->properties is more common and not easy to use. However, with curly braces (as shown in the previous example), it is easy to ensure that the same effect is achieved in PHP 5.6 and 7.
Performance
The biggest reason for upgrading to PHP 7 is performance gains, which are mainly due to the introduction of phpng changes. In fact, performance gains can lead to higher adoption rates, especially for small hosts that are generally reluctant to upgrade, and they are most likely to upgrade in order for the same machine to carry more customers.
So far, according to different benchmarks, PHP 7 has the same performance as Facebooks HHVM, which is characterized by the ability to compile PHP code into machine instructions (as long as possible) with the real-time (Just in) compiler.
PHP 7 does not have a JIT, although the discussion is abuzz. The number of performance improvements that can be made after adding a JIT is not yet known, but it's definitely fun if someone is interested in creating one!
In addition to performance improvements, you should save a lot of memory, because the optimization of internal data structures has always been the main way to improve performance.
Backwards incompatible changes
While internal developers try not to break backwards compatibility (BC), they are not always compatible when they want to advance the language.
However, these incompatibilities are minor, as is the case with the uniform variable syntax (Uniform Variable Syntax) that leads to broken backward compatibility, such as a catch fatal error that results when attempting to invoke a non-object method:
Set_error_handler (function ($code, $message) { var_dump ($code, $message); }); $var = null; $var->method (); echo $e->getmessage (); Fatal Error:call to a member function method () in null echo "Hello World";//will still run
In addition, the ASP and script tags have been deleted, which means that <% and <%= can no longer be used, or.
Other larger changes can be seen in all deprecated functions that have been removed.
The most important incompatibilities also include POSIX-compliant regular expression extensions, Ext/ereg (deprecated in version 5.3), and old Ext/mysql extensions (deprecated in 5.5 versions) removed.
Another minor incompatibility change is that it is not allowed to have multiple default cases in switch. Before PHP 7, the following are allowed:
Switch ($expr) { default: echo "Hello World"; break; Default: echo "Goodbye moon!"; break; }
This will cause only the latter to be executed. In PHP 7, this will result in:
Fatal error:switch statements may only contain one default clause-Switch syntax is allowed to contain only a single clause
New features
In the face of the impact of backward incompatibility, we have quite a few criticisms. The performance improvements have also cheered us up. But the thing that's most fascinated us is the new features! The new feature is the key to having fun with every release--php 7 does not lack new features.
Scalar type hints and return types
I'll be the first to introduce the most controversial changes that PHP 7 adds: scalar type hints. The addition of this feature did not vote at first. The author then withdrew the RFC. After that, many of the post-implementation-conflicting RFCs were raised, and after a public discussion, the original RFC was passed.
For you, the end user, this means that you can type hints (type-hint) on scalar types. Specifically, scalar types include: int,float,string, and BOOL. By default, type hints are not strict, which means they will force the original type to be converted to the type specified by the type hint. This means that if you pass int (1) to a function that needs a float type, it will change to float (1). The float (1.5) is passed into a function that requires the int type, and it becomes an int (1).
Here's an example:
function sendhttpstatus (int $statusCode, string $message) { header (' http/1.0 '. $statusCode. ". $message); } Sendhttpstatus (404, "File not Found"); Passed the shaping and string sendhttpstatus ("403", "OK");//String "403" strongly converted to int (403)
In addition, it will be declared declare (Strict_types=1); Placed at the top of any document, you can enable strict mode, and any function calls in the document must follow the specified type. Strict depends on the file that the function calls, not the function definition.
If a type hint does not match, a catch fatal error is thrown:
In addition, PHP 7 supports the return type hint, which supports all the same types as parameters. This follows the same syntax as hack, inserts a colon after the parentheses, and then the type:
function Isvalidstatuscode (int $statusCode): bool { return isset ($this->statuses[$statusCode]); }
In this example: bool indicates that the function will return a Boolean value.
The strict mode of the return type hint follows the same law as the type hint.
Synthetic comparison operators
My personal favorite PHP 7 new feature is the synthetic comparison operator, <=>, also known as the spaceship operator. Here I may be a person to like, because I wrote the original patch, also affected the naming (t_spaceship). But this is still a good addition to the PHP language, with the greater than and less than the operator form complementary.
In fact, the operator works in the same way as strcmp (), or version_compare (). If the left operand is less than the right, 1 is returned, and the two sides are equal to 0, and 1 if the left is greater than the right. The main difference is that it can be used between any two operands, not just a string, but also an integer, a floating-point number, an array, and so on.
The most common use of this operator is in a sort callback:
Pre spacefaring^w PHP 7 function Order_func ($a, $b) { return ($a < $b)? 1: (($a > $b)? 1:0); } //Post PHP 7 function Order_func ($a, $b) { return $a <=> $b; }
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.
Next
In this article, we learned about the most important non-compatibility fixes in PHP 7, which are already two big features.
In the next second article, we'll cover the other six features that are important in PHP 7. In addition, we'll cover some of the ways to help PHP 7 evolve at the end of the article series.
Original link: https://blog.engineyard.com/2015/what-to-expect-php-7