How to Upgrade PHP 5.X to PHP 7 Version

How to Upgrade PHP 5.X to PHP 7 Version

PHP 7 has been launched with new features and performance improvement. This post will cover the most important things to check on your code before upgrading to PHP 7.

PHP 7 New features:

  1. Constant arrays using define()
  2. Spaceship operator
  3. Null coalescing operator
  4. Return type declarations
  5. Scalar type declarations
  6. Anonymous classes and more

Deprecated Features

In PHP 7 `some methods or extensions are deprecated. So in PHP 7 you are not using these deprecated methods. Here is a list the deprecated methods.

Constructors With Name Same as Class

PHP 4 style constructors (methods that have the same name as the class they are defined in) are deprecated in PHP 7. To avoid this error you have made your constructor with __construct().

Deprecated

<?php
class Foo {
   function Foo() {
       echo 'I am the constructor';
   }
}
?>

Solution

To fix this issue change all the constructor names to function __constructor(). And other code will remain same.

<?php
class Foo {
   function __construct() {
       echo 'I am the constructor';
   }
}
?>

LDAP deprecations

ldap_sort() function has been deprecated in PHP 7.

Calling Static Methods

The Methods that are not declared as static and use as a static method are deprecated.

Deprecated

<?php
class foo {
    function bar() {
        echo 'I am not static!';
    }
}

foo::bar();
?>

Solution

<?php
class foo {
    public static function bar() {
        echo 'I am not static!';
    }
}

foo::bar();
?>

password_hash() salt option

To encrypt password, If you are using PHP password_hash() method with custom salt. Custom salt option has been deprecated in PHP 7. You need to use password_hash() without salt options.

Deprecated

<?php
$salt = [
    'cost' => 10,
    'salt' => mcrypt_create_iv(22, MCRYPT_DEV_URANDOM),
];
$password = password_hash("test", PASSWORD_BCRYPT, $salt);
echo $password;
?>

Solution

<?php
$salt = [
    'cost' => 10,
];
$password = password_hash("abcs", PASSWORD_BCRYPT, $salt);
echo $password;
?>

Leave a Reply

Your email address will not be published. Required fields are marked *