65

What's New in PHP 7.3 in 30 Seconds in Diffs | Tomas Votruba

 5 years ago
source link: https://www.tomasvotruba.cz/blog/2018/08/16/whats-new-in-php-73-in-30-seconds-in-diffs/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

What's New in PHP 7.3 in 30 Seconds in Diffs

No time but eager to hear PHP news? PHP 7.3 is out in December 2018 and it brings 173 changes. Which are the most useful ones?

From features that might be the most interesting to those lesser ones, that anybody will rarely use.

1. Comma After the Last Argument

  See RFC

Do you know this?

 $array = [
     1,
+    2,
 ];

We'll be able to do this:

 $this->someFunction(
     $arg,
+    $arg2,
 );

But still not this:

 function someFunction(
     $arg,
+    $arg2,
 ) {}

Thanks Jacob for pointing this difference out.

25 seconds to go...

2. First and Last Array Key

  See RFC

 $items = [
     1 => 'a',
     2 => 'b',
 ];

-reset($items);
-$firstKey = key($items);
+$firstKey = array_key_first($items);
 var_dump($firstKey); // 1
 $items = [
     1 => 'a',
     2 => 'b',
 ];

-end($items);
-$lastKey = key($items);
+$lastKey = array_key_last($items);
 var_dump($lastKey); // 2

These will be handy in coding standard tools.

Still 15 seconds...

3. Countable for Risky Variables

  See RFC

I don't think having a variable of 2 forms is a good idea:

<?php

$items = null; // same as "private $items;" in a class

echo sprintf('There is %d items', count($items));
// error Warning: count(): Parameter must be an array or an object that implements Countable

But in case of that smelly (3rd party) code, there is a help:

 $items = null;

+if (is_countable($items)) {
-echo sprintf('There is %d items', count($items));
+     echo sprintf('There is %d items', count($items));
+}

Only 5 seconds, hurry!

4. Safer JSON Parsing

  See RFC

-json_encode($data);
+json_encode($data, JSON_THROW_ON_ERROR);
-json_decode($json);
+json_decode($json, false, 512, JSON_THROW_ON_ERROR);

So you'll be able to do:

try {
    return json_decode($json, false, 512, JSON_THROW_ON_ERROR);
} catch (JsonException $exception) {
    // ...
}

I've used similar technique for years thanks to Nette\Utils and I've never complained:

<?php

try {
    return Nette\Utils\Json::encode($value);
} catch (Nette\Utils\JsonException $exception) {
    // ...
}

...0, you made it! Congrats, now get back to pwning the world!

Did I miss a feature you plan to use from day 1 of your PHP 7.3? I might too, drop it in the comments!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK