Scalar type declarations

<?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

More info here

Return type declarations

<?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 [];
}

More info here

Null coalescing operator "??"

<?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';

Spaceship operator

<?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.

Constant arrays using define()

<?php
/*
 * Simple as it sounds
 */
define('ANIMALS', [
    'dog',
    'cat',
    'bird'
]);

Anonymous classes

<?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;
    }
});

Unicode codepoint escape syntax

<?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}";

Closure::call

<?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

Filtered unserialize()

<?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]);

Group "use" declarations

<?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};

Generator delegation

<?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

Generator Return Expressions

<?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

Session options

<?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.

Hire me!

I'm currently open for new projects or join pretty much any project i may fit in, let me know!

Read about what I do, or contact me for details.