Some evolution from PHP 5.3 to PHP 7.3
<?php echo 5.3;
- Namespaces
- Goto
- Anonymous functions
- Constants can now be declared outside a class using the const keyword
- The ternary operator now has a shorthand form
?:
- Dynamic access to static methods is now possible
<?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.
<?php echo(5.5);
- Generators added via
yield
keyword - Try-catch blocks now support a
finally
block 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\n";
}
//output
A: 1; B: 2
A: 3; B: 4
- empty() supports arbitrary expressions
<?php
function always_false() {
return false;
}
if (empty(always_false())) {
echo "This will be printed.\n";
}if (empty(true)) {
echo "This will not be printed.\n";
}
- 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 "\n";
echo 'String dereferencing: ';
echo 'PHP'[0];
echo "\n";
//output
Array dereferencing: 1
String dereferencing: P
- Class name resolution via
::class
It is possible to useClassName::class
to get a fully qualified name of class ClassName
<?php
namespace Name\Space;
class ClassName {}
echo ClassName::class;
//output Name\Space\ClassName
<?= 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'."\n",
$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 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
function add($a, $b, $c) {
return $a + $b + $c;
}
$operators = [2, 3];
echo add(1, ...$operators);-
- Exponentiation via
**
- use function and use const, The
use
operator 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 Name\Space {
const FOO = 42;
function f() { echo __FUNCTION__."\n"; }
}
namespace {
use const Name\Space\FOO;
use function Name\Space\f;
echo FOO."\n";
f();
}
//output
42
Name\Space\f
__debugInfo();
check the doc
<?= 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"
- Anonymous classes
- Group
use
declaration
use some\namespace\ClassA;
use some\namespace\ClassB;
use some\namespace\ClassC as C;
use function some\namespace\fn_a;
use function some\namespace\fn_b;
use function some\namespace\fn_c;
use const some\namespace\ConstA;
use const some\namespace\ConstB;
use const some\namespace\ConstC;
use some\namespace\{ClassA, ClassB, ClassC as C};
use function some\namespace\{fn_a, fn_b, fn_c};
use const some\namespace\{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;
}
Multi catch exception handling
<?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);
- Allow trailing comma in function and method calls
<?php
function foo($a, $b,)
- Option to make
json_encode
andjson_decode
throw exceptions on errors
<?php
try {
json_decode("{", false, 512, JSON_THROW_ON_ERROR);
}
catch (\JsonException $exception) {
echo $exception->getMessage(); // echoes "Syntax error"
}