<?php
/*
* A few additon of how you can work using constants
*/
const ONE = 1; // Regular constant
const TWO = ONE * 2; // New feature
const ARR = ['a', 'b']; // Possible to define Array Constants
class C {
// As properties
const THREE = TWO + 1;
const ONE_THIRD = ONE / self::THREE;
onst SENTENCE = 'The value of THREE is '.self::THREE;
// As default function arguments
public function f($a = ONE + self::THREE) {
return $a;
}
}
echo (new C)->f(); // Outputs: 4
echo C::SENTENCE; // Outputs: The value of THREE is 3
<?php
/*
* This a new way to create functions with a variable number of arguments.
*/
function sum(...$numbers) {
$acc = 0;
foreach ($numbers as $n) {
$acc += $n;
}
return $acc;
}
echo sum(1, 2, 3, 4); // Outputs: 10
// ---------------------------------------------------------------
function f($req, $opt = null, ...$params) {
// $params is an array containing the remaining arguments.
echo count($params);
}
f(1); // outputs: 0
f(1, 2); // outputs: 0
f(1, 2, 3); // outputs: 1
f(1, 2, 3, 4); // outputs: 2
f(1, 2, 3, 4, 5); // outputs: 3
More info on Variadic functions
<?php
/*
* Arrays and Traversable objects can be unpacked into argument
* lists when calling functions by using the ... operator.
* This is called splat operator in other languages.
*/
function add($a, $b, $c) {
return $a + $b + $c;
}
$operators = [2, 3];
echo add(1, ...$operators);
<?php
/*
* A right associative ** operator has been added to support exponentiation,
* along with a **= shorthand assignment operator.
*/
echo 2 ** 3; // outputs 8
$a = 2;
$a **= 3; // $a value is 8
<?php
/*
* The use operator has been extended to support importing functions
* and constants in addition to classes.
*/
namespace Name\Space {
const FOO = 42;
function f() { return 0 }
}
namespace {
use const Name\Space\FOO;
use function Name\Space\f;
// .. Use FOO and f();
}
For a full list of changes go here.