|
1 PHP 5.3 new features
1.1 supports namespace (Namespace)
There is no doubt that the namespace is PHP5.3 brought about the most important new features.
In PHP5.3, only the need to specify a different namespace, the namespace separator is a backslash \.
//select.php
Php
namespace Zend \ Db \ Table;
class Select {
}
?>
Even though the presence of a class named Select other namespace in the call will not conflict. Readability of the code has also increased.
Call the method:
//call.php
Php
// Namespace Zend \ Db;
include ( 'select.php');
$ S = new Zend \ Db \ Table \ Select ();
$ S-> test ();
?>
1.2. Support delay static binding (Late Static Binding)
In PHP5, we can self keyword in the class or __CLASS__ judge or calling the current class. But there is a problem, if we are to call in the subclass, the result will be a parent. Since inheriting the parent class, static members had been bound. E.g:
Php
class A {
public static function who () {
echo __CLASS__;
}
public static function test () {
self :: who ();
}
}
class B extends A {
public static function who () {
echo __CLASS__;
}
}
B :: test ();
// The above code output result is: A; This is different from our expectations, we want the corresponding results for the original class.
PHP 5.3.0 adds a static keyword to refer to the current class, which implements the delay static binding:
Php
class A {
public static function who () {
echo __CLASS__;
}
public static function test () {
static :: who (); // here to achieve a statically bound delayed
}
}
class B extends A {
public static function who () {
echo __CLASS__;
}
}
B :: test ();
// Output the results of the above code is: B
1.3 supports the goto statement
Most computer programming languages are supported unconditionally turned goto statement, when the program executes the goto statement, goto statement that is shifted from the reference position indicated in the program to continue. Despite the goto statement may cause the program flow is not clear, readable weakened, but with the convenience of its unique place in certain circumstances, such as interrupts deeply 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) goto end;
}
}
echo "i = $ i";
end:
echo 'j hit 17';
1.4 supports closures, Lambda / Anonymous functions
Closures (Closure) function and Lambda functions in functional programming concepts from the field. For example, JavaScript is the most common language closures and lambda functions support. In PHP, we can also create a function in your code run through create_function (). But there is one problem: function created only when running compiled without simultaneously with other code is compiled into executable code, so we can not use this execution code similar APC caching to improve the efficiency of code execution. In PHP5.3, we can use Lambda / Anonymous functions to define some temporary use (ie use disposable type) function, as array_map () / array_walk () and other callback function.
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');
// ... To 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 new magic method __callStatic () and __invoke ()
PHP originally had a magic method __call (), when a method does not exist in the object code to call the magic method will be called automatically. New __callStatic () method are used only for static class methods. When attempting to call a static method in the class that does not exist, __ callStatic () magic method will be called automatically.
Php
class MethodTest {
public function __call ($ name, $ arguments) {
// $ Name parameter is case sensitive
.. Echo "calling object method '$ name'" implode ( '-', $ arguments) "\ n";
}
/ ** PHP 5.3.0 and later versions of the class method valid * /
public static function __callStatic ($ name, $ arguments) {
// $ Name parameter is case sensitive
.. Echo "static method call '$ name'" implode ( '-', $ arguments) "\ n";
}
}
$ Obj = new MethodTest;
$ Obj-> runTest ( 'by calling the object');
MethodTest :: runTest ( 'static invocation'); // As of PHP 5.3.0
// Output after executing the above code as follows:
// Call the object method 'runTest' - an object by calling
// Call the static method 'runTest' - static call
When to call a function in the form of an object, __ invoke () method will be called automatically.
Php
class MethodTest {
public function __call ($ name, $ arguments) {
// $ Name parameter is case sensitive
. Echo "Calling object method '$ name'" implode ( ',', $ arguments) "\ n".;
}
/ ** PHP 5.3.0 and later versions of the class method valid * /
public static function __callStatic ($ name, $ arguments) {
// $ Name parameter 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 New Nowdoc grammar
Usage and Heredoc similar, but use single quotes. Heredoc is required to declare by the use of double quotes. Nowdoc will not do any variable resolution, very suitable for passing a PHP script.
Php
After single quotes // Nowdoc support PHP 5.3
$ Name = 'MyName';
echo <<< 'EOT'
My name is "$ name".
EOT;
// The above code output My name is "$ name". ((Wherein the variables are not parsed)
// Heredoc unquoted
echo <<< FOOBAR
Hello World!
FOOBAR;
Support after // or double quotes PHP 5.3
echo <<< "FOOBAR"
Hello World!
FOOBAR;
Supported by Heredoc to initialize static variables, class members and class constants.
Php
// Static variables
function foo ()
{
static $ bar = <<< LABEL
Nothing in here ...
LABEL;
}
// Class members constants
class foo
{
const BAR = <<< FOOBAR
Constant example
FOOBAR;
public $ baz = <<< FOOBAR
Property example
FOOBAR;
}
1.7 outside the class can also be used to define constants with the const
// PHP constants defined in this way is usually
define ( "CONSTANT", "Hello world.");
// And a new way to define constants
const CONSTANT = 'Hello World';
1.8 ternary operator adds a shortcut way of writing
Originally format is (expr1) (expr2): (expr3); if expr1 result is True, it returns expr2 results?.
A writing PHP5.3 new way, you can omit the intermediate part, writing for the expr1: expr3; if expr1 result is True, then returns expr1 results?
// Original format
? $ Expr = $ expr1 $ expr1: $ expr2
// New format
$ Expr = $ expr1:? $ Expr2
1.9 HTTP status codes in the 200-399 range are considered successful visit
1.10 supports dynamic call the static method
class Test {
public static function testgo ()
{
echo "gogo!";
}
}
$ Class = 'Test';
$ Action = 'testgo';
$ Class :: $ action (); // output "gogo!"
2 PHP5.3 changed Other notable
1.1 fixes a lot of bug
1.2 PHP to improve performance
1.3 php.ini variable can be used
1.4 mysqlnd extend into the core theory of the extended access mysql rate than in previous MySQL and MySQLi fast expansion (see http://dev.mysql.com/downloads/connector/php-mysqlnd/)
1.5 ext / phar, ext / intl, ext / fileinfo, ext / sqlite3 and ext / enchant and other extensions by default with PHP bindings release. Phar PHP which can be used to package a program, similar to Java in the jar mechanism.
1.6 ereg regular expression functions are no longer available by default, please use the faster speed of the PCRE regular expression functions
3 deprecated features
PHP 5.3.0 Two new level of errors: E_DEPRECATED and E_USER_DEPRECATED error level E_DEPRECATED be used to illustrate a function or function has been deprecated E_USER_DEPRECATED level to signal that the user code deprecated feature, similar E_USER_ERROR and E_USER_WARNING.. grade.
The following is a list of instructions INI deprecated any instruction that will result in the following error E_DEPRECATED.
define_syslog_variables
register_globals
register_long_arrays
safe_mode
magic_quotes_gpc
magic_quotes_runtime
magic_quotes_sybase
Deprecated INI file with '#' at the beginning of the comment.
Deprecated functions:
call_user_method () (use call_user_func () replacement)
call_user_method_array () (use call_user_func_array () replacement)
define_syslog_variables ()
dl ()
ereg () (use preg_match () replacement)
ereg_replace () (use preg_replace () replacement)
eregi () (use preg_match () with 'i' modifier substitute)
eregi_replace () (use preg_replace () with 'i' modifier substitute)
set_magic_quotes_runtime () function and its alias magic_quotes_runtime ()
session_register () (use the $ _SESSION super all variable substitutions)
session_unregister () (use the $ _SESSION super all variable substitutions)
session_is_registered () (use the $ _SESSION super all variable substitutions)
set_socket_blocking () (use stream_set_blocking () replacement)
split () (use preg_split () replacement)
spliti () (use preg_split () with 'i' modifier substitute)
sql_regcase ()
mysql_db_query () (use mysql_select_db () and mysql_query () replacement)
mysql_escape_string () (use mysql_real_escape_string () replacement)
Waste transfer locale name as a string using the LC_ * constants series instead.
mktime () is is_dst parameters using the new timezone handling functions instead.
Deprecated features:
Deprecated the return value of new by reference distribution.
Passing by reference is deprecated call. |
|
|
|