PHP 7 – the new features at a glance – Part 2


Expectations

Expactations can mark places in your code, which should not be reached actually, But if it is, can leave a message in the production environment, to other programmers to debug and understand the codes to help. The parameter expression is evaluated and a false value will raise an AssertionException thrown.

Definition:

void assert (mixed $expression [, mixed $message]);

The php.ini configuration variable Zend.assertions can 3 Value be

  • 1 = Generate and execute code (development mode)
  • 0 = Generate code and around at jump it at runtime
  • -1 = don ’ t generate any code (zero-cost, production mode)

The php.ini configuration variable assert.exception has three values:

  • 1 = Throw exception, If the expression has the value of false
  • 0 = throw an exception, If the expression has the value of false

Example:

ini_set('zend.assertion', '1'); //in development mode
ini_set('assert.exception', '1'); //in development mode
public function method() {
    for (/*...*/) {

        if (/* ... */)
           return true;
    }
    assert(false, 'Error, something went wrong');
}

The exception thrown here only, If the php.ini Variable assert.exception is set (For example,. in the Test-Environment). This will help prevent, an perfomanceintensive exception where thrown in the Live-Environment.

Example 2:

This code throws an exception:

ini_set('assert.exception', '0');
ini_set('zend.assertions', '0');

assert(false, 'Some error message');

This, however, already

ini_set('assert.exception', '1');
ini_set('zend.assertions', '1');

assert(false, 'Some error message');
Fatal error: Some error message

Clustered use syntax

Use statements with the same parent namespace can be easily recorded and clearly summarized:

// Pre PHP 7 code
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;

// PHP 7+ code
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};

The generator syntax

A generator function looks just like a normal function except, that a generator instead of a value will return as many values as necessary.

This is the heart of a generator function yield-Keyword. In its simplest form, the yield keyword looks like a return statement, except that execution with the return will not stop, but instead yield a value for the code provides in the loop about the generator, as long as the execution of the generator function stops.

A generator can do not return values: do you do this, a compile-time error is returned. An empty return-Statement is a valid syntax within a generator and the generator will stop.

Those: php.net

 function generiere_eins_bis_drei() {
    for ($i = 1; $i <= 3; $i++) {
        // Hinweis: $i bleibt zwischen den yields erhalten.
        yield $i;
    }
}

$generator = generiere_eins_bis_drei();
foreach ($generator as $wert) {
    echo "$wert\n";
}

//Ouput:
1
2
3

The Intdiv radio tone – Division and integer spins in one

var_dump(intdiv(10, 3)); // int(3)

Session tuning

The well-known session_start() function can now 3 new parameter for more performance tuned to:

session_start([ 'read_and_close' => true, ]);

The session is read immediately after the closed again, so reading can be the fastest is blocking calls of the session.

It should be noted, that this may cause inconsistencies in competing write attempts to the session if request A and B lessen the session one at a time and save one after another new values in the session, in the last update request overrides the changes of the first request.

ini_set(session.lazy_write)

Is enabled by default, the session is only overwritten, If she also changed. Makes sense, If the session data from competing requests changes can, and must be written non-sequential.

Thus you can reach, that accesses overlapping (e.g.: through asynchronous requests) of the same block user when reading and writing the session.

The new preg_replace_callback_array() Function

The old function syntax preg_replace_callback() led to difficult readable, waste code and is by the new preg_replace_callback_array() improved.

    $tokenStream = [];

    $input = '$a = 3; // variable initialisation';

// Pre PHP 7 code
    preg_replace_callback(
        [
            '~\$[a-z_][a-z\d_]*~i',
            '~=~',
            '~[\d]+~',
            '~;~',
            '~//.*~'
        ],
        function ($match) use (&$tokenStream) {
            if (strpos($match[0], '$') === 0) {
                $tokenStream[] = ['T_VARIABLE', $match[0]];
            } elseif (strpos($match[0], '=') === 0) {
                $tokenStream[] = ['T_ASSIGN', $match[0]];
            } elseif (ctype_digit($match[0])) {
                $tokenStream[] = ['T_NUM', $match[0]];
            } elseif (strpos($match[0], ';') === 0) {
                $tokenStream[] = ['T_TERMINATE_STMT', $match[0]];
            } elseif (strpos($match[0], '//') === 0) {
                $tokenStream[] = ['T_COMMENT', $match[0]];
            }
        },
        $input
    );

// PHP 7+ code
    preg_replace_callback_array(
        [
            '~\$[a-z_][a-z\d_]*~i' => function ($match) use (&$tokenStream) {
                $tokenStream[] = ['T_VARIABLE', $match[0]];
            },
            '~=~' => function ($match) use (&$tokenStream) {
                $tokenStream[] = ['T_ASSIGN', $match[0]];
            },
            '~[\d]+~' => function ($match) use (&$tokenStream) {
                $tokenStream[] = ['T_NUM', $match[0]];
            },
            '~;~' => function ($match) use (&$tokenStream) {
                $tokenStream[] = ['T_TERMINATE_STMT', $match[0]];
            },
            '~//.*~' => function ($match) use (&$tokenStream) {
                $tokenStream[] = ['T_COMMENT', $match[0]];
            }
        ],
        $input
    );
    var_dump($tokenStream);

Result:

array (size=5)
  0 => 
    array (size=2)
      0 => string 'T_VARIABLE' (length=10)
      1 => string '$a' (length=2)
  1 => 
    array (size=2)
      0 => string 'T_ASSIGN' (length=8)
      1 => string '=' (length=1)
  2 => 
    array (size=2)
      0 => string 'T_NUM' (length=5)
      1 => string '3' (length=1)
  3 => 
    array (size=2)
      0 => string 'T_TERMINATE_STMT' (length=16)
      1 => string ';' (length=1)
  4 => 
    array (size=2)
      0 => string 'T_COMMENT' (length=9)
      1 => string '// variable initialisation' (length=26)

Cryptographically secure random numbers with random_int()

Definition:

int random_int(int min, int max);

If no safe random number can be generated, an exception is thrown.

The following system functions are used on different operating systems:

  • Under Windows CryptGenRandom()
  • Under Linux it is getrandom(2) syscall
  • On other platforms /dev/urandom

Define an array as a constant

 define('ALLOWED_FILE_TYPES', ['jpg', 'jpeg', 'gif', 'png']);

Using global reserved identifiers

Global reserved identifier can now be used as class variables or methods:

// 'new', 'private', and 'for' were previously unusable
Project::new('Project Name')->private()->for('purpose here');

 

Back to PHP 7 – the new features at a glance – Part 1