Home  >  Article  >  Backend Development  >  Precautions for handling PHP5 to PHP7 in one move

Precautions for handling PHP5 to PHP7 in one move

醉折花枝作酒筹
醉折花枝作酒筹forward
2021-06-08 09:15:213210browse

This article will introduce to you the precautions from PHP5 to PHP7. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

Precautions for handling PHP5 to PHP7 in one move

PHP7 is the general trend. PHP7 has more new features, better performance, and higher speed. Moreover, the beta3 of PHP7.0 will be released from August 7 this year, and the RC1 version of PHP7 will be released soon. The pace is getting closer, and more people are learning the new features of PHP7. PHP Academy will serialize the new features of PHP7.

Moreover, some domestic PHPers who like to try new things have already installed PHP7. Let's take a look at what new features PHP7 has.

Today we are going to explain some styles promoted by PHP, and we have disabled some styles in the original PHP4.

1. In order to please ASP programmers when PHP5 was released, PHP prepared all asp_tags to be banned.

There will be no 72637aecae1027e7d023ac098a170986 style in the future, and PHP will no longer support it. style statement.

<script language="php"></script>

2, some things in the syntax style of PHP4 will be completely abandoned in the new PHP7, such as the usage of constructors. PHP7 will prompt an error message: E_DEPRECATED.

<?php
class foo {
    //方法名类名相同的构造函数的用法不再兼容     
    function foo() {         
        echo &#39;I am the constructor&#39;;     
    } 
  }
?>

1. Define defined constants not only support scalars, but also arrays

<?php

define(&#39;PHPXY&#39;, array(
    &#39;凤姐&#39;,
    &#39;芙蓉姐姐&#39;,
    &#39;杨幂是臭脚&#39;
));

echo PHPXY[1]; // 输出的结果是“芙蓉姐姐”
?>

2. When comparing, it supports 96b4fef55684b9312718d5de63fb7121, which can compare strings and arrays. Integer type.

<?php

// Integers
echo 1 <=> 1; // 0
echo 1 <=> 2; // -1
echo 2 <=> 1; // 1

Comment: We believe that the actual users are not large, and the usage in actual work will not be very high

3. Mandatory parameter type specification of functions is supported in PHP7

<?php
// Coercive mode
function sumOfInts(int ...$ints)
{
    return array_sum($ints);
}

var_dump(sumOfInts(2, &#39;3&#39;, 4.1));

//本例中会输出9,你想想为什么哟?——因为强制规定为了整型

Comment: function funcName(... parameter) is supported by PHP5.6. Don't think it is a knowledge point in PHP7

4. The return type is also mandatory

function arraysSum(array ...$arrays): array
{
    return array_map(function(array $array): int {
        return array_sum($array);
    }, $arrays);
}

print_r(arraysSum([1,2,3], [4,5,6], [7,8,9]));

5. A new call method is added to be called externally, and the anonymous function is appended in a shorter way Enter the object and complete the call

class A {private $x = 1;}

// Pre PHP 7 code
$getXCB = function() {return $this->x;};
$getX = $getXCB->bindTo(new A, &#39;A&#39;); // intermediate closure
echo $getX();

// PHP 7+ code
$getX = function() {return $this->x;};
echo $getX->call(new A);

6. The member method can also stipulate that a certain class type must be returned

class A {}
class B extends A {}

class C
{
    public function test() : A
    {
        return new A;
    }
}

We must delete some functions of PHP7.

In the PHP manual, in order to specifically cope with your upgrade, some functions deleted in PHP7 are explained and organized for you. If you use these functions in your project, please search and modify them throughout the project.

1. call_user_method() and call_user_method_array()

2. mcrypt_generic_end() alias in favor of mcrypt_generic_deinit()

3. Deprecated mcrypt_ecb(), mcrypt_cbc() , mcrypt_cfb() and mcrypt_ofb() in favor of mcrypt_decrypt() parameters MCRYPT_MODE_*

4. Deprecated datefmt_set_timezone_id() and IntlDateFormatter::setTimeZoneID() in favor of datefmt_set_timezone() or IntlDateFormatter::setTimeZone()

5. set_magic_quotes_runtime() and its alias function magic_quotes_runtime()

6. set_socket_blocking() is beneficial to its alias function stream_set_blocking()

7. From fast- cgi's dl()

8. T1Lib supports deletion, so delete: imagepsbbox(), imagepsencodefont(), imagepsextendedfont(), imagepsfreefont(), imagepsloadfont(), imagepsslantfont(), imagepstext()

There was a slight delay in the original serialization plan of new features of PHP7. We have translated the official press release of the PHP7 RC1 version. This is the PHP7.0 RC1 version that everyone has been waiting for a long time.

The so-called RC version is the version that will be officially launched soon. (Release Candidate) is a candidate version when used in software. The system platform is the release candidate version. The RC version will not add new features and will mainly focus on debugging.

The PHP development team announced that version PHP 7.0.0 RC 1 is a soon-to-be-available version. You can test in detail and report the problems you encounter to PHP's BUG tracking system.

The address for BUG test submission is: https://bugs.php.net/

1. PHP7 is twice as fast as php 5.6

2. Update Good support for 64-bit operating systems

3. More error support and new error trapping

4. Remove some unsupported functions and unsupported SAPIs and extensions

5. Null coalescing operator (??)

6. Comprehensive comparison operator (96b4fef55684b9312718d5de63fb7121)

7. Return type declaration

8. Scalar type declaration

9. Anonymous class

PHP has deleted some functions, two of which are scary:

1. Regular functions of the ereg_* series

2. Mysql_* series of database connection functions

PHP7 completely removes Mysql extension support, and the original mysql_* series of functions will no longer be supported in mysql. Therefore, if your application system still uses the mysql_* series of functions to connect to the database, please upgrade your mysql series of functions as soon as possible.

Let’s take a look at what extensions have been deleted by PHP7:

  • 1. ereg

  • 2. mssql

  • 3. mysql

  • 4. sybase_ct

Alternative processing and solutions:

1. If you want to connect to Microsoft's sql server database, please use the PDO solution.

2. If you use the mysql series of function extensions to connect to the database, please use the more efficient mysql_nd series of functions. . It has higher efficiency

3. If you are using the ereg series, change it as soon as possible.

4. Change sybase_ct to sybase* series

Many friends don’t know what SAPI is, and they don’t know the relationship between SAPI and PHP. In this chapter, while understanding which SAPIs have been deleted in PHP7, you can learn more about the internal processing mechanism of PHP and what are the SAPI is deleted

SAPI refers to the programming interface for specific PHP applications. Just like PC, no matter which operating system is installed, as long as it meets the PC interface specifications, it can run normally on the PC. PHP scripts must There are many ways to execute it, through a web server, directly from the command line, or embedded in other programs.

  • aolserver

  • apache

  • apache_hooks

  • apache2filter

  • caudium

  • continuity

  • isapi

  • milter

  • nsapi

  • phttpd

  • pi3web

  • roxen

  • thttpd

  • tux

  • webjames

The above SAPIs will not be supported.

Recommended learning: php video tutorial

The above is the detailed content of Precautions for handling PHP5 to PHP7 in one move. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete