- What’s new in PHP 7.4? Top 10 features that you need to know
- Originally written by Daniel Dan
- The Nuggets translation Project
- Permanent link to this article: github.com/xitu/gold-m…
- Translator: Prince Stuart
- Proofreader: jiang wujie, suhanyujie
What’s new in PHP 7.4? 10 Features you must Master
In just seven days, we saw the release of PHP 7.4. The updates include: reduced memory usage and significant performance improvements. Take a look at the 10 main features of PHP 7.4 in this article.
Why some programming languages are so popular while others are rarely used in projects and sometimes forgotten. There are many reasons, such as the simplicity of syntax, the degree of functionalization, the impact of development networks, and community support on each level of technical requirements.
As IT continues to evolve around the world, coding technologies must respond to the changing environment by providing, updating, or enhancing new features. This is one of the most important factors in the success of a programming language.
At our company, PHP is constantly being improved and optimized for performance every year, so I love PHP and I believe it will be very popular in the years to come. Since PHP 5 was released in 2004, its performance has doubled, maybe even tripled, which is one of the reasons why our software development company uses PHP for development.
Unsurprisingly, PHP is one of the 10 most popular programming languages for the second year in a row, according to the 2019 StackOverflow Developer survey. This year, it ranked eighth, up one place from last year.
In just seven days, on Thursday, November 28, we will see the release of a new version of PHP – PHP 7.4 – that will be one of the most feature-rich releases ever. In this article, I’ll list and present an overview of the updated features for PHP 7.4. Let’s get started!
What’s new in PHP 7.4? PHP Feature List
1. Arrow function support
Because anonymous functions or closures are primarily used in JS, they can seem verbose in PHP, and their implementation and program maintenance can be more complex.
The introduction of support for arrow functions allowed PHP developers to clean up their code and make the syntax much cleaner. This will greatly improve the readability and simplicity of your code. Look at the following example.
So, in the old days, you would have to write the following code block:
function cube($n){
return ($n * $n * $n);
}
$a = [1.2.3.4.5];
$b = array_map('cube', $a);
print_r($b);
Copy the code
After PHP 7.4 is released, you can write as follows:
$a = [1.2.3.4.5];
$b = array_map(fn($n) => $n * $n * $n, $a);
print_r($b);
Copy the code
With the ability to create neat, shorter code. The Web development process will be faster and save you time.
2. Support for typed properties
The introduction of typed properties in the next release is likely to be seen as one of the most important feature updates for PHP. While previously it was impossible to use declared methods for class variables and properties (including static properties), programmers can now easily code without creating specific getters and setters.
Because of declared types (void and callable are not included), you can use Nullable types: int, float, array, String, Object, Iterable, self, bool, and parent.
If a Web developer tries to assign an unrelated value from a type, for example, declaring the name variable as a string, he or she will receive TypeError.
Like the arrow functions, typed properties also allow PHP engineers to write shorter and clearer code.
3. The preload
The main purpose of this cool new feature is to improve PHP 7.4 performance. In short, preloading is the process of loading files, frameworks, and libraries in OPcache and is definitely the best complement to the new version. For example, if you use a framework, you must download and recompile its files for each request.
These code files first participate in request processing when OPcache is configured, and then are checked for changes each time. Preloading enables the server to load specified code files into shared memory. It is important to note that they will always be available for all subsequent requests without checking for changes to other files.
It’s also worth noting that PHP eliminates unnecessary inclusion during preloading and addresses class dependencies and links with Traits and Interfaces, among other things.
4. Covariable returns and covariable arguments
Currently, PHP is mostly about immutable parameter types and immutable return types, which imposes some constraints. With the return of covariates (of type from more specific to more general) and the introduction of covariates (of type from more general to more specific) parameters, PHP developers will be able to change parameter types to one of the supertypes.
5. A weak reference
In PHP 7.4, weak reference classes allow Web developers to save links to objects that do not prevent them from being destroyed. Do not confuse weak-reference classes with weak-reference extensions. Because of these features, they make it easier to implement cache-like structures.
See an example of using this class:
$obj = new stdClass;
$weakref = WeakReference::create($obj);
var_dump($weakref->get());
unset($obj);
var_dump($weakref->get());
? >
Copy the code
Also, note that you cannot serialize weak references.
6. Merge allocation operators
Merge operators are another new feature in PHP 7.4. This is useful when you need to use ternary operators with isset methods. If it exists and is not empty, the first operand is returned, otherwise the second operand is returned.
Here is an example:
<? PHP / /$_GET['user'], and return nobody if it does not exist$username = $_GET['user']????'nobody'; // This is equivalent to:$username = isset($_GET['user'])?$_GET['user'] : 'nobody'; // chain merge: will return$_GET['user'],$_POST['user'] and the first non-null value in noboody$username = $_GET['user']????$_POST['user']????'nobody'; ? >Copy the code
7. Expansion operators in array expressions
In contrast to array_merge, in PHP 7.4 engineers were able to use expansion operators in arrays. There are two main reasons. First, the expansion operator is considered a language construct, whereas array_merge is a function, and second, compilation time optimization for constant arrays. As a result, PHP 7.4 performance will improve.
Look at an example of parameter decompression in an array expression:
$parts = ['apple'.'pear'];
$fruits = ['banana'.'orange'.$parts.'watermelon'];
var_dump($fruits);
Copy the code
It is also possible to extend the same array multiple times. In addition, PHP developers will be able to use the syntax in arrays because ordinary elements can be added before and after the extended operator.
8. New custom object serialization mechanism
In the new version of PHP, there are two new methods available: __serialize and __unserialize. Combining the versatility of the Serializable interface with the implementation of __sleep and __wakeup methods, this serialization mechanism allows PHP developers to avoid some custom problems with existing methods. Find out more about PHP features.
9. Reflection provided for references
Libraries such as Symfony/var-Dumper rely heavily on the Reflection API to list variables accurately. Originally, reference reflection was not well supported, forcing these libraries to rely on hacks to detect references. The ReflectionReference class was added in PHP 7.4 to address this problem.
10. Support throwing exceptions from __toString() methods
Previously, you could not throw an exception from the __toString method. The reason is that many functions in the library perform conversions from objects to strings, and not all of them are ready to “handle” exceptions properly. As part of this RFC, string conversions in the code base were thoroughly reviewed and this restriction was removed.
Final thoughts
In less than a week, PHP 7.4 will be released. There are a number of new PHP features that reduce memory usage and greatly improve PHP 7.4 performance. You’ll be able to avoid some of the limitations of this programming language, write cleaner code, and create Web solutions faster.
Beta 3 is now available for download and testing for the server. However, I do not recommend that you use it in a production environment or a project under development. If you’re still wondering about PHP 7.4 or PHP development, or just enjoyed this article, feel free to leave a comment below.
If you find any mistakes in your translation or other areas that need to be improved, you are welcome to the Nuggets Translation Program to revise and PR your translation, and you can also get the corresponding reward points. The permanent link to this article at the beginning of this article is the MarkDown link to this article on GitHub.
The Nuggets Translation Project is a community that translates quality Internet technical articles from English sharing articles on nuggets. The content covers Android, iOS, front-end, back-end, blockchain, products, design, artificial intelligence and other fields. If you want to see more high-quality translation, please continue to pay attention to the Translation plan of Digging Gold, the official Weibo, Zhihu column.