Some evolution from PHP 5.3 to PHP 7.3 📍

11 / 06 / 2018

<?php echo 5.3;

<?php
class C {
   public static $foo = 42;
}

$a = "C";
echo $a::$foo; //output 42

<?= 5.4;

  • Support for traits has been added
  • Short array syntax []has been added
<?php
$a = [1, 2, 3, 4]; 
$a = ['one' => 1, 'two' => 2, 'three' => 3, 'four' => 4];
  • Closures support $this
  • Class member access on instantiation has been added
    <?php
    class Calculator {
    public function add($a, $b) {
      return $a + $b;
    }
    }
    echo (new Calculator)->add(40, 2); //output 42
  • Built-in development [web server in CLI mode](web server in CLI mode.).

<?php echo(5.5);

  • Generators added via yield keyword
  • Try-catch blocks now support a finallyblock for code that should be run regardless of whether an exception has been thrown or not.
  • foreach now supports list()
<?php
$array = [
    [1, 2],
    [3, 4],
];

foreach ($array as list($a, $b)) {
    echo "A: $a; B: $b
";
}
//output
A: 1; B: 2
A: 3; B: 4
  • Array and string literals can now be dereferenced directly to access individual elements and characters
<?php
echo 'Array dereferencing: ';
echo [1, 2, 3][0];
echo "
";

echo 'String dereferencing: ';
echo 'PHP'[0];
echo "
";
//output
Array dereferencing: 1
String dereferencing: P
  • Class name resolution via ::classIt is possible to use ClassName::classto get a fully qualified name of class ClassName
<?php
namespace NameSpace;
class ClassName {}

echo ClassName::class;
//output NameSpaceClassName

<?= 5.6;

  • Constant expressions
<?php
const ONE = 1;
const TWO = ONE * 2;

class C {
    const THREE = TWO + 1;
    const ONE_THIRD = ONE / self::THREE;
    const SENTENCE = 'The value of THREE is '.self::THREE;

    public function f($a = ONE + self::THREE) {
        return $a;
    }
}

echo (new C)->f(); //output 4
echo C::SENTENCE; //output The value of THREE is 3
  • Variadic functions via . . .
    
    <?php
    function f($req, $opt = null, ...$params) {
    // $params is an array containing the remaining arguments.
    printf('$req: %d; $opt: %d; number of params: %d'."
    ",
           $req, $opt, count($params));
    }

f(1); f(1, 2); f(1, 2, 3); f(1, 2, 3, 4); f(1, 2, 3, 4, 5);

- [Argument unpacking](http://php.net/manual/en/migration56.new-features.php#migration56.new-features.splat) via `. . .` Arrays and Traversable objects can be unpacked into argument lists when calling functions by using the … operator. This is also known as the splat operator in other languages, including Ruby.

```php
<?php
function add($a, $b, $c) {
    return $a + $b + $c;
}

$operators = [2, 3];
echo add(1, ...$operators);
  • Exponentiation via **
  • use function and use const, The useoperator has been extended to support importing functions and constants in addition to classes. This is achieved via the use functionand use const constructs, respectively
<?php
namespace NameSpace {
    const FOO = 42;
    function f() { echo __FUNCTION__."
"; }
}

namespace {
    use const NameSpaceFOO;
    use function NameSpacef;

    echo FOO."
";
    f();
}
//output 
42
NameSpacef

<?= 7.0;

<?php
$username =  $_GET['user'] ?? 'nobody';
// This is equivalent to:
$username = isset($_GET['user']) ? $_GET['user'] : 'nobody';
// Integers
echo 1 <=> 1; // 0
echo 1 <=> 2; // -1
echo 2 <=> 1; // 1

// Floats
echo 1.5 <=> 1.5; // 0
echo 1.5 <=> 2.5; // -1
echo 2.5 <=> 1.5; // 1

// Strings
echo "a" <=> "a"; // 0
echo "a" <=> "b"; // -1
echo "b" <=> "a"; // 1
  • Constant arrays using define()
<?php 
define('ANIMALS', [
    'dog',
    'cat',
    'bird'
]);

echo ANIMALS[1]; // outputs "cat"
use some
amespaceClassA;
use some
amespaceClassB;
use some
amespaceClassC as C;

use function some
amespacefn_a;
use function some
amespacefn_b;
use function some
amespacefn_c;

use const some
amespaceConstA;
use const some
amespaceConstB;
use const some
amespaceConstC;

use some
amespace{ClassA, ClassB, ClassC as C};
use function some
amespace{fn_a, fn_b, fn_c};
use const some
amespace{ConstA, ConstB, ConstC};

<?= 7.1;

  • Nullable types, Type declarations for parameters and return values can now be marked as nullable by prefixing the type name with a question mark.
<?php

function testReturn(): ?string
{
    return 'elePHPant';
}

var_dump(testReturn());

function testReturn(): ?string
{
    return null;
}

var_dump(testReturn());

function test(?string $name)
{
    var_dump($name);
}

test('elePHPant');
test(null);
test();
//output
string(10) "elePHPant"
NULL
string(10) "elePHPant"
NULL
Uncaught Error: Too few arguments to function test(), 0 passed in...
<?php
class ConstDemo
{
    const PUBLIC_CONST_A = 1;
    public const PUBLIC_CONST_B = 2;
    protected const PROTECTED_CONST = 3;
    private const PRIVATE_CONST = 4;
}

<?php
try {
    // some code
} catch (FirstException | SecondException $e) {
    // handle first and second exceptions
}

<?= 7.2;

<?php

function test(object $obj) : object
{
    return new SplQueue();
}

test(new StdClass());
<?php

abstract class A
{
    abstract function test(string $s);
}
abstract class B extends A
{
    // overridden - still maintaining contravariance for parameters and covariance for return
    abstract function test($s) : int;
}

<?= 7.3 (BETA);

original post here

  • Allow trailing comma in function and method calls
<?php
function foo($a, $b,)
  • Option to make json_encode and json_decode throw exceptions on errors
<?php
try {
  json_decode("{", false, 512, JSON_THROW_ON_ERROR);
}
catch (JsonException $exception) {
  echo $exception->getMessage(); // echoes "Syntax error"
}
Thanks for reading, I hope you liked it, don't forget to share it !
LinkedIn Twitter