PHP 5.3 New Features

Source: Internet
Author: User
Tags constant definition ereg

PHP 5.3 New Features

1 New features in PHP 5.3

1.1 Namespace)

Namespace is undoubtedly the most important new feature of PHP5.3.

In PHP5.3, you only need to specify a different namespace. The namespace separator is reversed \.

// Select. php
<? Php
Namespace Zend \ Db \ Table;
Class Select {

}
?>

In this way, even if the Select class exists in other namespaces, the program will not conflict during the call. The readability of the Code is also increased.
Call method:

// Call. php
<? Php
// Namespace Zend \ Db;
Include ('select. php ');
$ S = new Zend \ Db \ Table \ Select ();
$ S-> test ();
?>

1.2. Support for Late Static Binding)

In PHP5, we can use the self keyword or _ CLASS _ in the CLASS to determine or call the current CLASS. But there is a problem. If we call it in the subclass, the result will be the parent class. Because when inheriting the parent class, the static member has been bound. For example:

<? Php
Class {
Public static function who (){
Echo _ CLASS __;
}
Public static function test (){
Self: who ();
}
}
Class B extends {
Public static function who (){
Echo _ CLASS __;
}
}
B: test ();

// The output result of the above Code is: A; this is different from our expectation. We originally wanted to get the corresponding result of the subclass.

A static keyword is added to PHP 5.3.0 to reference the current class, that is, delayed static binding is implemented:

<? Php
Class {
Public static function who (){
Echo _ CLASS __;
}
Public static function test (){
Static: who (); // latency static binding is implemented here
}
}
Class B extends {
Public static function who (){
Echo _ CLASS __;
}
}

B: test ();

// The output result of the above Code is: B

1.3 Support for goto statements

Most computer programming languages support unconditional redirection to the goto statement. When the program is executed to the goto statement, the program is switched to the position indicated by the label in the goto statement to continue execution. Although a goto statement may lead to unclear procedures and reduced readability, It has unique conveniences in some cases, such as interrupting nested loops and if statements.

<? Php
 
Goto;
Echo 'foo ';
A:
Echo 'bar ';
For ($ I = 0, $ j = 50; $ I <100; $ I ++ ){
While ($ j --){
If ($ j = 17) goto end;
}
}
Echo "I = $ I ";
End:
Echo 'J hit 17 ';

1.4 support closure and Lambda/Anonymous Functions

Closure functions and Lambda functions come from the functional programming field. For example, JavaScript is one of the most common languages that support closures and lambda functions. In PHP, you can also use create_function () to create a function during code execution. However, there is a problem: the created function is compiled only at runtime, instead of being compiled into Execution Code at the same time as other code, therefore, we cannot use Execution Code caches like APC to improve code execution efficiency. In PHP5.3, we can use Lambda/anonymous functions to define some temporarily used (disposable) functions as Callback functions of functions such as array_map ()/array_walk.

<? Php
Echo 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 _. ": PRICE _". strtoupper ($ product ));
$ Total + = ($ pricePerItem * $ quantity) * ($ tax + 1.0 );
};
Array_walk ($ products, $ callback );

1.5 two magic methods are added: _ callStatic () and _ invoke ()

PHP originally had a magic method _ call (). When the code calls a non-existent method of the object, the magic method is automatically called. The new _ callStatic () method is only used for static class methods. When you try to call a static method that does not exist in the class, the __callstatic () magic method is automatically called.

<? Php
Class MethodTest {
Public function _ call ($ name, $ arguments ){
// The parameter $ name is case sensitive.
Echo "Call object method '$ name'". implode (' -- ', $ arguments). "\ n ";
}
/** This class method is valid in PHP 5.3.0 and later versions */
Public static function _ callStatic ($ name, $ arguments ){
// The parameter $ name is case sensitive.
Echo "Call the static method '$ name'". implode (' -- ', $ arguments). "\ n ";
}
}

$ Obj = new MethodTest;
$ Obj-> runTest ('call through object ');
MethodTest: runTest ('static call'); // As of PHP 5.3.0

// After the above code is executed, the output is as follows:
// Call the object method 'runtest' -- call through the object
// Call the static method 'runtest' -- Static call

When an object is called as a function, the __invoke () method is automatically called.

<? Php
Class MethodTest {
Public function _ call ($ name, $ arguments ){
// The parameter $ name is case sensitive.
Echo "Calling object method '$ name'". implode (', ', $ arguments). "\ n ";
}

/** This class method is valid in PHP 5.3.0 and later versions */
Public static function _ callStatic ($ name, $ arguments ){
// The parameter $ name is case sensitive.
Echo "Calling static method '$ name'". implode (', ', $ arguments). "\ n ";
}
}
$ Obj = new MethodTest;
$ Obj-> runTest ('in object context ');
MethodTest: runTest ('in static context'); // As of PHP 5.3.0

1.6 Add Nowdoc syntax

Similar to Heredoc, but with single quotes. Heredoc must be declared using double quotation marks. Nowdoc does not parse any variables, so it is very suitable for passing a piece of PHP code.

<? Php
// Nowdoc supported after single quotes PHP 5.3
$ Name = 'myname ';
Echo <'eot'
My name is "$ name ".
EOT;
// The above code outputs My name is "$ name". (The variable is not parsed)
// Heredoc without quotation marks
Echo <FOOBAR
Hello World!
FOOBAR;
// Or double quotation marks supported after PHP 5.3
Echo <"FOOBAR"
Hello World!
FOOBAR;

You can use Heredoc to initialize static variables, class members, and class constants.

<? Php
// Static variables
Function foo ()
{
Static $ bar = <LABEL
Nothing in here...
LABEL;
}
// Class members and constants
Class foo
{
Const BAR = <FOOBAR
Constant example
FOOBAR;

Public $ baz = <FOOBAR
Property example
FOOBAR;
}

1.7 you can also use const to define constants outside the class.

// This method is usually used to define constants in PHP.
Define ("CONSTANT", "Hello world .");

// And a constant definition method is added.
Const CONSTANT = 'Hello world ';

The 1.8 ternary operator adds a quick Writing Method

The original format is (expr1 )? (Expr2): (expr3); If expr1 returns True, expr2 is returned.
PHP5.3 adds a writing method that can omit the intermediate part and write as expr1? : Expr3; if the expr1 result is True, the result of expr1 is returned.

// Original format
$ Expr = $ expr1? $ Expr1: $ expr2
// New format
$ Expr = $ expr1? : $ Expr2

1.9 HTTP status code-is considered successful

1.10 support dynamic call of static methods

Class Test {
Public static function testgo ()
{
Echo "gogo! ";
}
}
$ Class = 'test ';
$ Action = 'testgo ';
$ Class: $ action (); // output "gogo! "

2. Other noteworthy changes in PHP5.3

1.1 fixed a large number of bugs
1.2 PHP Performance Improvement
1.3 variables available in php. ini
1.4 mysqlnd entry core extension theoretically this extension accesses mysql faster than the previous MySQL and MySQLi extensions (see http://dev.mysql.com/downloads/connector/php-mysqlnd)
1.5 ext/phar, ext/intl, ext/fileinfo, ext/sqlite3, ext/enchant, and other extensions are released with PHP binding by default. 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 faster PCRE Regular Expression functions.

3. Abandon the Function

PHP 5.3.0 adds two error levels: E_DEPRECATED and E_USER_DEPRECATED. error level E_DEPRECATED indicates that a function or function has been deprecated. the purpose of the E_USER_DEPRECATED level is to indicate the discarding function in user code, which is similar to the E_USER_ERROR and E_USER_WARNING levels.

The following is the list of discarded INI commands. using any of the following commands will cause the E_DEPRECATED error.
Define_syslog_variables
Register_globals
Register_long_arrays
Safe_mode
Magic_quotes_gpc
Magic_quotes_runtime
Magic_quotes_sybase
Discard comments starting with '#' in the INI file.

Abandon function:
Call_user_method () (use call_user_func)
Call_user_method_array () (use call_user_func_array)
Define_syslog_variables ()
Dl ()
Ereg () (use preg_match () instead)
Ereg_replace () (use preg_replace)
Eregi () (Replaced by preg_match () and 'I' modifier)
Eregi_replace () (use preg_replace () in combination with the 'I' modifier)
Set_magic_quotes_runtime () and its alias function magic_quotes_runtime ()
Session_register () (use $ _ SESSION to replace all variables)
Session_unregister () (use $ _ SESSION to replace all variables)
Session_is_registered () (use $ _ SESSION to replace all variables)
Set_socket_blocking () (use stream_set_blocking)
Split () (use preg_split () instead)
Spliti () (Replaced by preg_split () and 'I' modifier)
SQL _regcase ()
Mysql_db_query () (replace mysql_select_db () with mysql_query)
Mysql_escape_string () (use mysql_real_escape_string)
The name of the Region passed by the string is discarded. Use the LC _ * series constant instead.
The is_dst parameter of mktime (). It is replaced by the new time zone handler.
Features discarded:
Discard the return value of allocating new by referencing.
When the call is passed, the reference is discarded.

 

Install LNMP in CentOS 6.3 (PHP 5.4, MyySQL5.6)

Nginx startup failure occurs during LNMP deployment.

Ubuntu install Nginx php5-fpm MySQL (LNMP environment setup)

Detailed php hd scanning PDF + CD source code + full set of teaching videos

Configure the php lnmp development environment in CentOS 6

PHP details: click here
PHP: click here

This article permanently updates the link address:

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.