Generators Introduced

<?php
/*
 * A generator allows you to write code that uses foreach
 * to iterate over a set of data without needing to build
 * an array in memory, which may cause you to exceed a memory
 * limit, or require a considerable amount of processing time
 * to generate. Instead, you can write a generator function,
 * which is the same as a normal function, except that instead
 * of returning once, a generator can yield as many times as it
 * needs to in order to provide the values to be iterated over. 
 */

function getLines($file) {
  $f = fopen($file, 'r');
  try {
    while ($line = fgets($f)) {
      yield $line;
    }
  } finally {
     fclose($f);
  }
}

foreach (getLines("file.txt") as $n => $line) {
    if ($n > 5) break;
    echo $line;
}

More info about Generators here

Finally keyword added

try {
  // Some dangerous code...
} catch (Exception $e) {
  // Handle the error here...
} finally {
  // This will be executed always
}

New password hashing API

<?php

// To create a password hash
$myHash = password_hash("mysupersecretpassword", PASSWORD_DEFAULT);

// To verify that a password matches the hash
$isValid = password_verify("mysupersecretpassword", $myHash);

More info about hashing here

Foreach now supports list()

<?php
/*
 * The foreach control structure now supports unpacking nested
 * arrays into separate variables via the list() construct. 
 */
$array = [
  [1, 2],
  [3, 4],
];

foreach ($array as list($a, $b)) {
  echo "A: $a; B: $b\n";
}

More info about foreach here

empty() supports arbitrary expressions

<?php
/*
 * In the past empty() used to accept only variables, now you can do stuff like:
 */
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";
}

Class name resolution via ::class

<?php
/*
 *  Gets a fully qualified name of class
 */
namespace Name\Space;
class ClassName {}

// outputs: Name\Space\ClassName
echo ClassName::class;

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.