<?php
// Coercive Mode (default)
function sumOfInts(int ...$ints){
return array_sum($ints);
}
// These will be coerced to integers and output int(9)
var_dump(sumOfInts(2, '3', 4.1));
<?php
// Strict Mode
declare(strict_types=1);
function sum(int $a, int $b) {
return $a + $b;
}
var_dump(sum(1.5, 2.5)); // This will throw an error
<?php
/*
* It's pretty much the same as argument type declarations,
* and also the same types are available for return.
*/
function arraysSum(array ...$arrays): array
{
return [];
}
<?php
/*
* It returns its first operand if it exists and is not NULL;
* otherwise it returns its second operand.
*/
// Fetches the value of $_GET['user'] and returns 'nobody'
// if it does not exist.
$username = $_GET['user'] ?? 'nobody';
// This is equivalent to:
$username = isset($_GET['user']) ? $_GET['user'] : 'nobody';
// It also can be chained
$username = $_GET['user'] ?? $_POST['user'] ?? 'nobody';
<?php
/*
* It returns -1, 0 or 1 when $a is respectively
* less than, equal to, or greater than $b
*/
// 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
More about type comparison rules.
<?php
/*
* Simple as it sounds
*/
define('ANIMALS', [
'dog',
'cat',
'bird'
]);
<?php
/*
* These can be used in place of full class definitions for
* throwaway objects: the syntax is literally "new class" keywords
*/
interface Logger {
public function log(string $msg);
}
class Application {
private $logger;
public function getLogger(): Logger {
return $this->logger;
}
public function setLogger(Logger $logger) {
$this->logger = $logger;
}
}
$app = new Application;
//---- Here comes the magic using the class keyword
$app->setLogger(new class implements Logger {
public function log(string $msg) {
echo $msg;
}
});
<?php
/* Takes a Unicode codepoint in hexadecimal form, and outputs
* that codepoint in UTF-8 to a double-quoted string
*/
echo "\u{aa}";
echo "\u{0000aa}";
echo "\u{9999}";
<?php
/*
* Shorthand to bind an Closure to an object
*/
class A {private $x = 1;}
$getX = function() {return $this->x;};
echo $getX->call(new A); // Outputs: 1
<?php
// Provides better security
// converts all objects into __PHP_Incomplete_Class object
$data = unserialize($foo, ["allowed_classes" => false]);
// Converts only the specified classes
$data = unserialize($foo, ["allowed_classes" => ["MyClass", "MyClass2"]]);
// Default behaviour: Accepts all classes
$data = unserialize($foo, ["allowed_classes" => true]);
<?php
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};
<?php
function gen(){
yield 1;
yield 2;
// delegate to another one
yield from gen2();
}
function gen2(){
yield 3;
yield 4;
}
foreach (gen() as $val){
echo $val, PHP_EOL;
}
// Outputs: 1,2,3,4
<?php
/* Enables a return statement to be used within a generator
* to enable for a final expression to be returned.
* Here is how you access it
*/
$gen = (function() {
yield 1;
yield 2;
return 3;
})();
foreach ($gen as $val){
echo $val, PHP_EOL;
}
echo $gen->getReturn(); // Outputs: 3
<?php
/*
* session_start accepts an array of options to override session configuration
*/
session_start([
'cache_limiter' => 'private',
'read_and_close' => true,
]);
For a full list of changes go here.