New features in PHP 5.3

Source: Internet
Author: User
Tags ereg vars zend

1 new features in PHP 5.3 1.1 support for namespaces (Namespace)

There is no doubt that the namespace is the most important new feature brought by PHP5.3.

in PHP5.3, you only need to specify a different namespace, and the delimiter for the namespace is the backslash \.

select.php
<? php namespace zend\db\table; class Select {
?>

This way, even if a class named Select exists under other namespaces, the program does not create a conflict at the time of the call. The readability of the code has also increased.
Call Method:

Call. php<? PHP         // namespace zend\db;        include (' select.php ');         $s New Zend\db\table\select ();         $s
?>

1.2. Support delay static binding (late static bindings)

In PHP5, we can use the Self keyword or __class__ to determine or invoke the current class in a class. But there is a problem, if we are called in the subclass, the resulting result will be the parent class. Since the parent class is inherited, the static member is already bound. For example:

<?PHPclassA { Public Static functionWho () {Echo __class__; }         Public Static functionTest () { self::Who (); }    }    classBextendsA { Public Static functionWho () {Echo __class__; }} B:: Test ();

The result of the above code output is: A; This is different from what we expected, and we originally wanted the corresponding result of the subclass.


PHP 5.3.0 Adds a static keyword to refer to the current class, which implements deferred static binding :

<?PHPclassA { Public Static functionWho () {Echo __class__; }         Public Static functionTest () {Static:: Who ();//this implements a static binding of delay    }    }    classBextendsA { Public Static functionWho () {Echo __class__; }} B:: Test ();

The result of the above code output is: B
1.3 Support for GOTO statements

In most computer programming languages, the unconditional turn statement Goto is supported, and when the program executes to a goto statement, it moves to the program location indicated by the label in the Goto statement to continue execution. Although the goto statement may cause the program process to be unclear and less readable, in some cases it has its own unique convenience, such as breaking deep nested loops and if statements.

<?PHP
Goto A; Echo' Foo '; A:Echo' Bar '; for($i=0,$j= 50;$i<100;$i++) { while($j--) { if($j==17) GotoEnd; } } Echo"I =$i"; End:Echo' J hit 17 ';
1.4 Support for closures, lambda/anonymous functions

The concept of closure (Closure) functions and lambda functions comes from the field of functional programming. For example, JavaScript is one of the most common languages that support closures and lambda functions. In PHP, we can also create functions through create_function () when the code is running. But there is a problem: The created function is compiled only at run time, not with other code being compiled into execution code at the same time, so we cannot use execution code caching like APC to improve the efficiency of code execution. In PHP5.3, we can use the lambda/anonymous function to define a number of functions that are temporarily used (that is, disposable) to function as callback functions for functions such as Array_map ()/array_walk ().

<?PHPEcho Preg_replace_callback(' ~-([A-z]) ~ ',function($match) {            return Strtoupper($match[1]); }, ' Hello-world '); //Output HelloWorld
$greet=function($name) { printf("Hello%s\r\n",$name); }; $greet(' World '); $greet(' PHP ');
//... In a class $callback=function($quantity,$product) Use($tax, &$total) { $pricePerItem=constant(__class__. "::P rice_".Strtoupper($product)); $total+= ($pricePerItem*$quantity) * ($tax+ 1.0); }; Array_walk($products,$callback);
1.5 Added two magic methods __callstatic () and __invoke ()

PHP originally had a magic method __call (), and the Magic method would be called automatically when the code called a non-existent method of the object. The new __callstatic () method is used only for static class methods. The __callstatic () Magic method is called automatically when attempting to invoke a static method that does not exist in the class.

<?PHPclassMethodtest { Public function__call ($name,$arguments) {            //parameter $name case sensitive        Echo"Call Object Methods"$name‘ " .implode(‘ -- ‘,$arguments). "\ n"; }        /** PHP 5.3.0 or later, this method is valid*/         Public Static function__callstatic ($name,$arguments) {            //parameter $name case sensitive        Echo"Invoke static Method"$name‘ " .implode(‘ -- ‘,$arguments). "\ n"; }    }      $obj=Newmethodtest; $obj->runtest (' Call through object '); Methodtest:: Runtest (' static call ');//As of PHP 5.3.0

The above code executes after the output is as follows:
Call object method ' Runtest ' –-through object invocation
Call static method ' Runtest ' –-static call

The __invoke () method is called automatically when the object is called as a function.

<?PHPclassMethodtest { Public function__call ($name,$arguments) {            //parameter $name case sensitive        Echo"Calling object Method"$name‘ " .implode(‘, ‘,$arguments). "\ n"; }                /** PHP 5.3.0 or later, this method is valid*/         Public Static function__callstatic ($name,$arguments) {            //parameter $name case sensitive        Echo"Calling static Method"$name‘ " .implode(‘, ‘,$arguments). "\ n"; }    }    $obj=Newmethodtest; $obj->runtest (' in object context ')); Methodtest:: Runtest (' in static context ');//As of PHP 5.3.0
1.6 New Nowdoc syntax

Usage is similar to Heredoc, but uses single quotation marks. Heredoc need to be declared by using double quotation marks. Nowdoc does not do any variable parsing and is ideal for passing a piece of PHP code.

<?php // nowdoc single quote PHP 5.3        Then support  $name = ' MyName ' echo <<< ' EOT '  My name is " $name ".  EOT; //echo <<<foobar Hello world!  FOOBAR; // or double quotes after PHP 5.3 support  Hello world!                

Supports initialization of static variables, class members, and class constants through Heredoc.

<?PHP//Static Variablesfunctionfoo () {Static $bar= <<<LABEL Nothing in here...LABEL; }    //class members, constantsclassFoo {ConstBAR = <<<FOOBARConstantexample FOOBAR;  Public $baz= <<<FOOBAR Property Example FOOBAR; }
1.7 Const can also be used to define constants outside the class
// The constants defined in PHP are usually used this  way Define ("CONSTANT", "Hello world.") );     // and a new way  of defining constants Const CONSTANT = ' Hello world ';
1.8 ternary operator adds a quick way to write

The original format is (EXPR1)? (EXPR2): (EXPR3); If the expr1 result is true, the result of the EXPR2 is returned.
PHP5.3 New Writing method, you can omit the middle part, written as expr1?: EXPR3; Returns the result of Expr1 if the expr1 result is true

// Original Format   $expr=$expr 1? $expr 1:$expr 2  // new format   $expr=$expr 1?: $expr 2
1.9 HTTP status code is considered to have access success in the 200-399 range 1.10 supports dynamic call to static methods
class test{        publicstaticfunction  testgo ()        {             echo " Gogo! " ;        }    }     $class = ' Test ';     $action = ' Testgo ';     $class::$action();  // output "gogo!"   
2 Other notable changes in PHP5.3

1.1 Fixed a number of bugs
1.2 PHP Performance Improvements
1.3 php.ini can be used in a variable
1.4 Mysqlnd into the core extension in theory, the extension accesses MySQL faster than the previous MySQL and mysqli extensions (see http://dev.mysql.com/downloads/connector/php-mysqlnd/)
Extensions such as 1.5 Ext/phar, Ext/intl, Ext/fileinfo, Ext/sqlite3, and Ext/enchant are released by default with PHP bindings. Where Phar can be used to package PHP programs, similar to the jar mechanism in Java.

1.6 Ereg Regular expression functions are no longer available by default, use a faster pcre regular expression function

3 deprecated Features

PHP 5.3.0 has added two error levels: E_deprecated and e_user_deprecated. The error level e_deprecated is used to indicate that a function or function has been deprecated. The e_user_deprecated level is intended to indicate deprecated functionality in user code, similar to the E_user_error and e_user_warning levels.

The following is a list of deprecated INI directives . Using any of the following directives will result in a e_deprecated error.
Define_syslog_variables
Register_globals
Register_long_arrays
Safe_mode
Magic_quotes_gpc
Magic_quotes_runtime
Magic_quotes_sybase
Discard the comments that begin with ' # ' in the INI file.


Deprecated Functions :
Call_user_method () (use Call_user_func () instead)
Call_user_method_array () (use Call_user_func_array () instead)
Define_syslog_variables ()
DL ()
Ereg () (use Preg_match () instead)
Ereg_replace () (use Preg_replace () instead)
Eregi () (Replace with Preg_match () with ' i ' modifier)
Eregi_replace () (Replace with preg_replace () with ' i ' modifier)
Set_magic_quotes_runtime () and its alias function magic_quotes_runtime ()
Session_register () (Replace with $_session all variables)
Session_unregister () (Replace with $_session all variables)
Session_is_registered () (Replace with $_session all variables)
Set_socket_blocking () (use Stream_set_blocking () instead)
Split () (using Preg_split () instead)
Spliti () (Replace with Preg_split () with ' i ' modifier)
Sql_regcase ()
Mysql_db_query () (Use mysql_select_db () and mysql_query () overrides)
Mysql_escape_string () (use Mysql_real_escape_string () instead)
Obsolete to pass the locale name as a string. Use the Lc_* series constants instead.
The IS_DST parameter of the Mktime (). Use the new time zone handler function override.
Deprecated features :
Discards the return value assigned to new by reference.
The pass-through reference is deprecated when invoked.

New features in PHP 5.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.