New features of PHP5.3

Source: Internet
Author: User
New features of PHP5.3: read the new features of PHP5.3. The biggest change to Namespacesphp5.3 is undoubtedly Namespaces (a related PHPNamespacesFAQ was previously provided ). This has brought many benefits to php developers, and the function naming problem widely criticized has also been solved. Code is clearer. For common code earlier than 5.3, you need to customize it. "> <LIN

Namespaces
The biggest change to php 5.3 is undoubtedly Namespaces (a related FAQ about PHP Namespaces ). This has brought many benefits to php developers, and the function naming problem widely criticized has also been solved.

Clearer code
Common Code earlier than 5.3 requires a custom prefix to distinguish between functions and class names

PLAIN TEXT

CODE:

Function MY_wrapper (){}
Class MY_DB {}
Define ('My _ CONN_STR ','');
MY_wrapper ();
New MY_DB ();
MY_CONN_STR;
 

After namespace is used, the code looks more clean.

PLAIN TEXT

CODE:

Namespace MY;
Function wrapper (){}
Class DB {}
Const CONN_STR = '';
Use my as my;
Wrapper ();
New DB ();
CONN_STR;
Multiple namespaces are defined in a file.
What should I do if a file defines multiple namespaces?

PLAIN TEXT

CODE:

Namespace LIB;
Class MySQL {}
Class SQLite {}
$ B = new SQLite ();
Namespace LIB_EXTRA;
Class MScrypt {}
$ A = new MScrypt ();
Var_dump (
Get_class ($ ),
Get_class ($ B)
);
The above code is output:

PLAIN TEXT

CODE:

String (18) "LIB_EXTRA: MScrypt"
String (11) "LIB: SQLite"
Php is the language for interpreting execution, and the above results are reasonable.

Namespace priority
The functions, classes, and constants defined in namespace take precedence over the global ones.

PLAIN TEXT

CODE:

Namespace foo;
Function strlen ($ foo) {return htmlentities ($ foo );}
Echo strlen ("test"); // test
Echo: strlen ("test"); // 4
Echo namespace: strlen ("test"); // test
Friendship between namespace and autoload
Autoload parses the class file location based on the namespace name and class name
Autoload is triggered only when the class definition is not found in the namespace and global range.
The _ autoload defined in namespace is not automatically called.
PLAIN TEXT
 
CODE:
Function _ autoload ($ var) {var_dump ($ var);} // LIB: foo
Require "./ns. php ";/*
        Namespace LIB;
New foo ();
*/
Namespace accessories
PLAIN TEXT

CODE:

Namespace really: long: pointlessly: verbose: ns;
_ NAMESPACE __; // The new magic constant, indicating the name of the current namespace.
Class {}
Get_class (new a (); // really: long: pointlessly: verbose: ns:
Use really: long: pointlessly: verbose: ns: a AS B; // reference a class from the namespace
Note: the content here is excerpted from pdfIntroduction to PHP 5.3 Slides, which will not be repeated later.

Performance improvement
The overall performance of php 5.3 has been improved by 5-15%.

Md5 () faster than 10-15%
Better stack implementation in the engine
Moving Constants to read-only memory
Exception handling Process Improvement (simplified with fewer opcodes)
(Require/include) _ once improvement, remove repeated open
Smaller binary size & startup size with gcc4
New language features _ DIR __
Before 5.3, in order to obtain the Directory of the current script, the compile function call is required.

PLAIN TEXT

CODE:

Echo dirname (_ FILE _); // <PHP 5.3
 

In 5.3, only one magic constant _ DIR _ is required.

PLAIN TEXT

CODE:

Echo _ DIR __; //> = PHP 5.3
? : Operator
Convenient? : Operator. You can quickly obtain non-null values from two values/expressions.

PLAIN TEXT

CODE:

$ A = true? : False; // true
$ A = false? : True; // true
$ A = ""? : 1; // 1
$ A = 0? : 2; // 2
$ A = array ()? : Array (1); // array (1 );
$ A = strlen ("")? : Strlen ("a"); // 1
_ CallStatic ()
The magic method _ callStatic is added, which is similar to the function of _ call, but only valid for the static method.

PLAIN TEXT

CODE:

Class helper {
Static function _ callStatic ($ name, $ args ){
Echo $ name. '('. implode (',', $ args ).')';
        }
}
Helper: test ("foo", "bar"); // test (foo, bar)
Dynamic Call of static method
Dynamic Call of static methods? Dynamic and static combination.

PLAIN TEXT

CODE:

Class helper {
Static function foo () {echo _ METHOD __;}
}
$ A = "helper ";
$ B = "foo ";
$ A: $ B (); // helper: foo
Late Static Binding
If you do not know how to translate, it is easier to leave an original article. The timing of Event Processing for static methods has changed. Previously, events were handled during compilation and now events are processed during execution.

Before php 5.3, the following code will output A, but this is not what we need. The whoami method has been redefined in class B. It should have output B to conform to our thinking.

PLAIN TEXT

CODE:

Class {
Public static function whoami (){
Echo _ CLASS __;
   }
Public static function identity (){
Self: whoami ();
  }
}
Class B extends {
Public static function whoami (){
Echo _ CLASS __;
   }
}
B: identity (); // A <-- PHP <5.3
The following code uses static: whoami () to call static methods. After php 5.3, because _ CLASS _ is processed during the execution period, class B can be successfully captured in this example.

PLAIN TEXT

CODE:

Class {
Public static function whoami (){
Echo _ CLASS __;
   }
Public static function identity (){
Static: whoami ();
   }
}
Class B extends {
Public static function whoami (){
Echo _ CLASS __;
   }
}
B: identity (); // B <--> = PHP 5.3
Mysqlnd
See mysqlnd as the default mysql driver in php 5.3.

However, PDO_MySQL does not support mysqlnd yet. Currently, only mysql (I) extension is supported.

The new features of php 5.3 introduced earlier are convenient for developers. The following describes the features that virtual host providers like.

Enhanced INI file support
CGI/FastCGI supports INI configurations similar to. htaccess
You can set INI in each directory. The ini file name depends on php. ini configuration, but the setting in the [PATH =/var/www/domain.com] and [HOST = www.domain.com] sections cannot be modified.
Enhanced error handling
Variables and constants can be defined in the INI file and can be called directly in the program.
Attached an example of an INI file

PLAIN TEXT

CODE:

# User-defined php. ini file name (. htaccess). The default value is ". user. ini"
User_ini.filename = ". user. ini"
 
# If you want to disable this feature, set it to a null value.
User_ini.filename =
 
# The time-to-live (TTL) of the custom php. Ini file, measured in seconds. The default value is 300 seconds.
User_ini.cache_ttl = 300
 
[PATH =/var/www/domain.com]
Variables_order = GPC
Safe_mode = 1
 
[My variables]
Somevar = "1234"
Anothervar =$ {somevar}; anothervar = somevar
 
[Ini arrays]
Foo [bar] = 1
Foo [123] = 2
Foo [] = 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.