Differences between PHPv5.3 and PHP5.2

Source: Internet
Author: User
Tags dbase ereg php windows sybase sybase client expression engine microsoft iis
PHPV5.3 has been released for a long time. The "New Features in PHPV5.3" series will continue to introduce the exciting new features provided by this release. Part 1 introduces the changes made to object-oriented programming and object processing in PHP5.3, and part 2 introduces closure functions and lambda functions. The namespace is discussed in Part 1, which is the most promising version of PHP.

Introduction

This series focuses on new features in PHP V5.3, such as namespaces, closures, Object Management, object-oriented programming, and Phar. Although these attractive new features are widely welcomed by the language, PHP V5.3 is also designed to further optimize PHP. It is built on the popular and stable PHP V5.2 and is enhanced to make it more powerful. In this article, we will learn about the changes in PHP V5.3 and some things to consider when upgrading from PHP V5.2 to PHP V5.3.

Syntax changes

The language adds namespaces and closures (discussed in sections 2nd and 3rd) and more reserved words. Starting from PHP V5.3, namespace is no longer used as an identifier. The closure class is now a reserved class, but it can still be used as a valid identifier. Listing 1 shows some examples. due to the new reserved words, some statements are no longer valid in PHP V5.3.

Listing 1. invalid PHP statements

// The function definition below will throw a fatal error in PHP 5.3, but is perfectly

// Valid in 5.2 function namespace (){.... }

// Same with this class definition class Closure {.... }

Support for goto statements is also added in PHP V5.3. Now, goto is a reserved word. Goto statements are not very common in modern languages (you may remember how to use goto in BASIC), but in some cases, goto statements are indeed convenient. Listing 2 shows an example of how to use the goto statement.

Listing 2. goto statements in PHP

Echo "This text will get outputted ";

Goto;

Echo "This text will get skipped ";

A: echo "This text will get outputted ";

One possible use case of goto is to interrupt the loop of deep nesting and the if statement. This will make the code much clearer.

Changes in functions and methods

In PHP V5.3, functions and methods have not changed significantly, but some enhancements have been made to help solve some outstanding problems in PHP and improve performance. This section discusses some significant changes.

In earlier versions of PHP, the array functions atsort, natcasesort, usort, uasort, uksort, array_flip, and array_unique can pass objects rather than arrays in the form of parameters. Then, these functions treat the object attributes as the keys and values of the array. PHP V5.3 does not support this, so you need to first convert the object to an array. Listing 3 shows how to modify the code.

Listing 3. modifying code for some functions and converting objects into arrays

$ Obj = new stdClass;

$ Obj-> a = '1 ′;

$ Obj-> B = '2 ′;

$ Obj-> c = '3 ′;

Print_r (array_flip ($ obj ));

// Will NOT work in PHP 5.3, but will in PHP 5.2 print_r (array_flip (array) $ obj ));

// Will work in PHP 5.3 and 5.2

The magic method is now more restrictive. The following method must have public visibility: _ get _ set _ isset _ unset _ call. now, when _ call is used in a static context, to cope with the above changes, you can use the new _ callStatic () magic method. Except for the _ isString () magic methods that do not accept parameters, the required parameters of these methods are mandatory and must be provided. Listing 4 shows how to use these methods and their required parameters.

Listing 4. use magic methods

Class Foo

{

Public function _ get ($ key ){}

// Must be public and have one parameter

Public function _ set ($ key, $ val ){}

// Must be public and have two parameters

Public function _ toString (){}

// Must be public and have no parameters

}

On Windows, some functions are not supported in PHP before and are now supported in PHP V5.3.

For example, the getopt () function is used to parse the options used when the PHP script is called from the command line. The inet_ntop () and inet_ton () functions used to encode and decode Internet addresses can also be found in Windows? . There are also some mathematical functions, such as asinh (), acossh (), atanh (), log1p (), and expm1 (), which are also supported on Windows.

Scaling changes

PHP Extension C Library (PECL) has always been the source of new extensions in PHP. When an extension is mature and stable and can be considered as a useful feature in the core release, it is usually added in major version changes. According to this rule, from PHP V5.3, the following extensions will become part of the core PHP release. FileInfo provides functions to help detect file content types and encoding. these functions detect some magic byte character sequences in the file. IntlInternational Components for Unicode (ICU) Library is a package that provides functions for unicode and global support.

A php archive tool discussed in Phar section 4th.

Mysqlnd is a local PHP driver used for MySQL database access. it is a substitute for MySQL and MySQLi extensions that used libmysql library in the early days. SQLite3 is used to use a library of the SQLite V3 database.

An extension is usually transferred to PECL when it is no longer actively maintained or deemed not worth releasing along with the core PHP release. During PHP V5.3 transformation, the following extensions were kicked out of the core PHP release and maintained in PECL.

Ncurses simulates curses and displays graphical output on the command line.

Fpdf is used to construct and use Form and Form data in PDF documents.

Dbase supports reading and writing dbase compatible files.

Fbsql supports database access on the Frontbase database server.

Ming is an open-source library used to create Flash 4 animations.

Sybase extensions have been completely removed and replaced by sybase_ct extensions.

Sybase_ct extension is fully compatible with the former and should be a simple alternative (drop-in replacement ). This updated feature uses the Sybase client library, which needs to be installed on the Web server.

Build changes

PHP V5.3 focuses on improving the build process, so it is easier to build PHP on all platforms. To maintain consistency between PHP builds and provide a set of reliable components, PCRE, Reflection, and SPL extensions are no longer disabled during build. Now you can build distributed PHP applications that will use these extensions and ensure they are available. A new team took over the PHP Windows build last year. This team will provide some improvements for users on Windows. The new build will be based on the 586 architecture (Intel? Pentium? Or higher) as the target, and requires Windows 2000/XP or later. In addition, the support for Windows 98/NT and earlier versions is removed. Use Microsoft? Visual Studio? 2008 build PHP and x86-64 architecture. When used with FastCGI on Microsoft IIS Web server or with Apache, they provide higher performance when building with the same compiler and architecture. The Windows installer will also be improved to better configure PHP on the Microsoft IIS Web server. . Ini changes

An important feature of PHP is that it can be configured using the. ini file. In PHP V5.3, some problematic commands related to this file have been deleted, such as zend. zemo-compatibility_mode. Now, the flexibility of this file has been greatly improved.

There are two major improvements to the php. ini file:

Variables can be used in the php. ini file. This is very convenient for reducing the file redundancy, and it is more convenient to update the file if necessary. Listing 5 shows an example.

Listing 5. variables in the php. ini file

Foo = bar

[Section]

Newfoo =$ {bar}

Foo and newfoo have the same value.

Like setting with the Apache configuration file, you can set per-directory and per-site PHP ini. The advantage of doing so is that the syntax is consistent among all different SAPIs that can run PHP. Listing 6 shows how to configure PHP ini.

Listing 6. per-site and per-directory. ini settings

[PATH =/var/www/site1]

; Directives here only apply to PHP files in the/var/www/site1 directory

[HOST = www.example.com]

; Directives here only apply to PHP files requested from the www.example.com site.

You can also create these. ini commands in the. ini file specified by the user in the file system, just like the. htaccess file on the Apache HTTP Web server. The default file name is specified by the user_ini.filename command. You can disable this feature by setting this command to a null value. In the user-specified. ini file, no per-site or per-directory commands can be overwritten.

The abandoned feature PHP V5.3 has officially abandoned some older functions and will not be provided in future PHP versions. When using these functions, the E_DEPRECATED error occurs.

The following functions are abandoned in PHP V5.3:

Tick (declare (ticks = N) and register_tick_function () are used to call a function every time the parser executes n statements in the declare () block. They will be abolished because their functions have a lot of interruptions and this feature is rarely used.

Define_syslog_variables (), which initializes all syslog-related variables. This function is not required because the constant it defines has been globally defined. It is necessary to abolish this function call.

Ereg regular expression function. We recommend that you replace the PCRE regular expression functions because they are faster and more consistent with the regular expressions used in other languages and applications. The support for ereg functions will be abolished so that PHP can use a regular expression engine in a standardized manner.

We recommend that you remove these features when migrating to PHP V5.3. In the future, major PHP releases will not support the above features.

In conclusion, PHP V5.3 has many new features and also clears some content. There are also some backward compatibility issues. This article provides some guidance for migrating Web applications to PHP V5.3.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.