未加星标

New Features of PHP 7.3 [Complete Guide]

字体大小 | |
[开发(php) 所属分类 开发(php) | 发布者 店小二05 | 时间 2019 | 作者 红领巾 ] 0人收藏点击收藏
Follow the Series

You can check the other blog posts following the links below:

Introduction tophp 7.3 and new Syntax

New features of PHP 7.3

Deprecation and Changes ― not published yet, get the ebook if you want to read it now

New Features JSON_THROW_ON_ERROR

Not having an adequate way to handle errors when using JSON has been a problem for quite a long time, and web developers all over the world have seen this as a huge downside of the language,

The RFC of PHP 7.3 has accepted this update by a 23-0 vote, which shows how much this feature has been requested.

Until PHP v7.2, we needed to use a workaround to get an error from JSON and it was neither reliable nor proficient at its job.

Here is an example:

json_decode("{"); json_last_error() === JSON_ERROR_NONE // the result is false json_last_error_msg() // The result is "Syntax error"

Yes, we can determinate if our JSON had an error, but, this is clearly not the best method to do so.

The new flag I am about to show you is an excellent alternative because it gives programmers the opportunity to use the power of exceptions that can be managed within the “try-catch” block of code.

Let’s see a practical example, shall we?

use JsonException; try { $json = json_encode("{", JSON_THROW_ON_ERROR); return base64_encode($json); } catch (JsonException $e) { throw new EncryptException('Could not encrypt the data.', 0, $e); }

As you can see from the previous code block, the json_encode function nowhas an optional parameter JSON_THROW_ON_ERROR which will catch the error and display it using the following exception methods:

$e->getMessage(); // like json_last_error_msg() $e->getCode(); // like json_last_error()

The default adaptation of PHP 7.3 toward your existing code will be neutral and since it is an optional parameter after you update your PHP everything will still work as aspected.

This is one of the most important features of the update, so if you want to dig in and learn more have a look at the official RFC for JSON_THROW_ON_ERROR

is_countable

A countable element in your code can be a variable with an array format or an object whose class is implementing the countable interface.

Last year, PHP 7.2 added a warning that shows up whenever adeveloper was counting or trying to loop an uncountable element.

It is possible to solve this problem and one of the best solutions currently used is to apply a check like a snippet below:

if (is_array($santa) || $santa instanceof Countable) { // $santa is countable }

The code checks whether the variable is an array or is an instance of the Countable interface.

And it will work but it seems a little bit “crowded” and, as many of you that work long hours, after a while seeing these kinds of lines wears your eyes out.

The team that is developing the new version took account of this and added a new function that will help the web developer immensely.

The is_countable function takes a variable as a parameter and then returns a boolean depending on if the function is countable or not.

There is no restriction about the format the parameter has to be, of course, if you put a non-countable variable the return will be false.

Let’s see it in practice:

if (is_countable($santa)) { // $santa is countable }

This snippet basically does the same thing as the previous one but is much easier to remember and, most importantly for me, more readable.

Not to mention you can use this function as shown or in a ternary conditional operator which will look even more satisfying.

For more information about is_countable have a look at the official RFC

array_key_first(), array_key_last()

As per version 5.6 of PHP, there are over 75 built-in functions that belong to the arrays category.

Despite the vast numbers of tools available, at the moment, if we need to retrieve the first or the last key of an array we have to get all the keys using array_keys() and only then go for the first or last values of the array.

Another way is to opt for end() or reset() .

As you may know, all the methods just described modify the array pointer, which is something that (other than a resources consumer) you just may not want to do.

The RFC of the upcoming version proposed the introduction of four brand-new methods the were set to solve this issue.

The four methods were:

array_key_first() array_key_last() array_value_first() array_value_last()

They work for both numeric and associative arrays.

Here is some example of a numeric one:

$reindeers = [ 1 => "Rudolph", 2 => "Dasher", 3 => "Dancer", 4 => "Prancer", 5 => "Vixen", 6 => "Comet", 7 => "Cupid", 8 => "Donner", 9 => "Blitzen"]; $first = array_key_first($reindeers); // $first is equal to 1 $last = array_key_last($reindeers); // $last is equal to 9

This, instead, is an example of an associative array:

$reindeers = [ "Rudolph" => 1, "Dasher" => 2, "Dancer" => 3, "Prancer" => 4, "Vixen" => 5, "Comet" => 6, "Cupid" => 7, "Donner" => 8, "Blitzen" => 9]; $first = array_key_first($reindeers); // $first is equal to "Rudolph" $last = array_key_last($reindeers); // $last is equal to "Blitzen"

The same would have worked for the other two functions illustrated in this chapter, array_value_* .

Just to be clear,those functions have been refused with 18 nos and 15 yeses.

In my opinion, these two functions would have been useful as well but according to several web developers, in certain cases, the value returned would have been ambiguous.

Here is why:

$reindeers = []; $first = array_value_first($reindeers ); // $first is equal to null $last = array_value_last($reindeers );// $last is equal to null An additional option that I came across while browsing on forums and talking to other web developers was to return a tuple like [$key => $value] .

Even though this option will not be available on the new version, seeing the favorable responses, it might arrive with the following RFCs.

Since this is a function that did not exist before, there are not going to be any backward compatibility problems, the only issues arrive if you have created and you are using your own version of array_key_first() and array_key_

本文开发(php)相关术语:php代码审计工具 php开发工程师 移动开发者大会 移动互联网开发 web开发工程师 软件开发流程 软件开发工程师

代码区博客精选文章
分页:12
转载请注明
本文标题:New Features of PHP 7.3 [Complete Guide]
本站链接:https://www.codesec.net/view/628592.html


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 开发(php) | 评论(0) | 阅读(322)