Back to the article list

02 Feb 2017

Updating deprecation rules for PHP 7.2

A few days ago, the PHP 7.2 deprecation RFC proposed by Nikita Popov was accepted, meaning that several deprecations will be added to the PHP engine in 7.2. Therefore, today we updated the SensioLabsInsight deprecation rules in order to match these changes.

__autoload

The __autoload is now deprecated in favor of spl_autoload_register as it is much more flexible allowing chained autoloaders to be registered and as these two autoload registration methods are incompatible.

function __autoload($className) {
    include $className.'.php';
}

// Should be replaced by

spl_autoload_register(function($className) {
    include $className.'.php';
});

create_function

create_function was a technique used before PHP 5.3 and based on eval to create lambda functions. Due to its nature, this function is not performant and is not needed anymore. It should not be used anymore.

create_function('$a, $b', 'return strlen($b) - strlen($a);');

// Should be replaced by

function($a, $b) {
    return strlen($b) - strlen($a);
};

gmp_random

gmp_random generates a random number between 0 and 2**($n*BITS_PER_LIMB)-1, where BITS_PER_LIMB is platform specific and not accessible for userland code. Therefore, this function is tied to the platform and should not be used.

PHP 5.6 introduced gmp_random_bits() and gmp_random_range() functions to solve this issue. These functions should always be preferred over gmp_random().

gmp_random();

// Should be replaced by

gmp_random_range($min, $max); // or
gmp_random_bits($bits);

each

each is a technique that can be used to iterate over an array, similarly to foreach. However, it is inferior to foreach in every way, being 10 times slower and posing problems to the engine. Therefore, foreach should always be preferred.

while (list($key, $val) = each($array)) {
    echo $key.' => '.$val.'<br />';
}

// Should be replaced by

foreach ($array as $key => $value) {
    echo $key.' => '.$val.'<br />';
}

Don’t hesitate to drop us a message if you face any issue.

Enjoy!

comments powered by Disqus