New features in PHP 5.3 and summary of deprecated function functions

Source: Internet
Author: User
Tags zend framework
There is no doubt that PHP has become one of the hottest technologies in web-prescribing today. According to Nexen.net's survey, One-third of Web sites on the Internet choose PHP to develop server-side programs. In countries such as Europe, America and Japan, the PHP development market presents a thriving scene, with a plethora of well-known PHP sites such as Facebook, Yahoo!, Flickr and SourceForge. In recent years, major websites in China have also been using PHP extensively.

Relying on an active and well-organized development community, the PHP language itself has been steadily progressing-improving performance and stability on the one hand, adding useful development tools, and actively absorbing the advantages of other programming languages to enrich language features. Today's PHP, which supports powerful object-oriented development (such as Java), retains easy-to-learn syntax (such as C), while PHP has a very diverse range of useful functions, extensions, and class libraries that are very handy for web development. In addition, with the gradual popularization of object-oriented development, a variety of open source PHP class Library and development framework emerge.

At the end of June 09, PHP officially released the PHP5.3.0. This is an unusual version of PHP because it fixes a number of bugs (more than 140) and brings a lot of new features that the PHP programmers have long awaited. Some of these features were originally planned for release in PHP6, but were released early in the PHP5.3 because of the high level of outcry.

Let's take a look at some good things in PHP5.3.

A Deprecated functions and functions in PHP 5.3

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.



Deprecated functions:

Two What's new in PHP 5.3

1. Support Namespace (Namespace) 2. Supports lazy static bindings (late static binding) 3. Supports goto statement 4. Support Closure, lambda/anonymous function 5. Added two magic methods __callstatic () and __invoke () 6. Added Nowdoc Syntax 7. You can also use const to define a constant 8 outside of a class. The ternary operator adds a quick way to write: 9. HTTP status codes are considered to be successful in the 200-399 range 10. Dynamic call to static method 11. New __DIR__ Magic Variable

1. Support Namespaces (Namespace)
There is no doubt that the namespace is the most important new feature brought by PHP5.3. With the concept of namespaces, it is easier to design a flexible structure when developing large sites, while avoiding conflicting class names or variable names in different packages.

Before PHP5.3, the usual way to divide the package is to separate the code files by their directory names, and the class names in the code to represent the directories with underscores _. For example
PHP code

 
    


This naming method is widely used by pear, Zend framework and various PHP projects. While this approach avoids conflicting class names in different packages or class libraries, it can be cumbersome and cumbersome to write code.
In PHP5.3, you only need to specify a different namespace, and the delimiter for the namespace is the backslash.
select.php
PHP code

 
    


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.
Calling methods
call.php
PHP code

 
  Test ();  ? >  


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:
PHP code

 
    


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:
PHP code

 
    


The result of the above code output is:
B
3. Support Goto Statement
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 code

 
    


4. Support 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 ().
PHP code

 
    


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.
PHP code

 
  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 the object method ' Runtest ' –-call the static method ' Runtest ' –-a static call through an object call


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

 
  


In this case, the direct use of the object name when the function is used, called the _invoke method;
Output
Test


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 code

 
    


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

 
    



7. Const can also be used to define constants outside the class
The constants defined in PHP are usually in this way:
PHP code

 
    


And a new way of defining constants is added:
PHP code

 
    


8. Ternary operator adds a quick way to write
PHP code

?:  


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

9. HTTP status code is considered to be successful in the 200-399 range
10. Support Dynamic Call static method
PHP code

 
    



11. Support for nesting exception handling (Exception)
12. New garbage Collector (GC), and enabled by default

Conclusion:
PHP 5.3 is a much improved version of PHP, but it still follows the design principles of PHP-powerful and easy to use. PHP5.3 on the one hand in the aspect of object-oriented development and so on, make PHP more suitable for enterprise application development, on the other hand also added a lot of practical grammatical features and new extensions. We look forward to its early stability and become another tool in web development.

  • 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.