PHP 5.4 Syntax improvements and deprecated features _php tutorial

Source: Internet
Author: User
Tags deprecated line web traits
PHP 5.4 was settled this month, and it was the first major update to PHP since 2009. The language section has been enhanced to include support for Traits and removal of some of the controversial features.

Traits

Like Java and. NET, PHP uses a single inheritance model. While this model is adequate for most use case scenarios, sometimes users also need to put some common boilerplate code in other unrelated classes. (The. NET Dispose method is a good example of this type of boilerplate code (boiler-plate).) To solve this problem, PHP5.4 launched the Traits.

Traits is a collection of scenarios that does not belong to any actual class. Instead of creating Trait instances or calling methods directly in Trait, users must merge Traits into the actual class to use them. In terms of precedence, the Trait method overrides the inherited method of the same name, and the method with the same name in the current merge class overrides the Trait method.

Traits has the same priority, so by default it is an error to add two Traits that contain overlapping method names to the same class. Of course, using the "Insteadof" and "as" operators can resolve conflicts manually, thus avoiding this error. In addition, the "as" operator can also be used to change the visibility of the Trait (visibility).

The methods in Traits can be abstract methods to support partial generalization of Traits, and Traits can expose static variables externally, and each class containing Traits will have a copy of the variable. In addition, a property that has been declared in Traits can no longer be declared in the class that contains it.

Other syntax improvements

Other syntax improvements include:

    • Arrays can now be declared with square brackets, instead of using the "array" function. The new way of declaring has no influence other than a slight brevity;
    • It is now possible to take an array value directly on the function return value. Until then, the result of the function return value must first be stored in a temporary variable, such as: $FirstName = GetNames () <0>;
    • Closures in PHP 5.4 can now use the "$this" pointer, which was not possible in the past;
    • Class members can now be accessed at instantiation, such as: (New CustomerService)->delete (customerId);
    • The binary number is identified by the "0b" prefix.

Command-line WEB server

PHP 5.4 provides its own WEB server, which you can launch from the command line. The Web server is designed only to help develop, and it does not replace your production Web server.

Deprecated features

The highly criticized Register Globals has been completely removed from PHP. For ten years, this feature has been known for its frequent security breaches. 2002 This feature is set to off by default. PHP5.3, released in 2009, marked this feature as "deprecated", presumably since then, most developers have no longer used it.

Another feature that is not pleasing to remove from PHP is the Magic Quotes. Magic Quotes is intended to be an automatic escape of strings (escape) in an attempt to avoid SQL injection attacks. However, because the escape method of the string is often associated with a particular background, it creates more problems than the problem that is being tried to solve. This feature, like Register Globals, was also marked as "deprecated" in 2009.

The break and continue statements in PHP can be followed by a parameter to indicate the number of loop layers to jump out. If you do not specify a parameter, it jumps out of the inner loop like VB, C #, or Java. Before PHP 5.4, developers could pass a variable to the break statement, and now only pass constants.

PHP allows parameters to be passed by reference. In earlier versions, you could indicate that a variable was passed by reference by adding a decoration to the call point. In PHP 5.4, this option has been removed. Instead, modern PHP programming only needs to be passed by reference when the function is declared. Unlike C #, you do not need to specify the pass by reference at both the Declaration and the invocation point.

View English Original: http://www.infoq.com/news/2012/03/PHP-5.4

http://www.bkjia.com/PHPjc/752330.html www.bkjia.com true http://www.bkjia.com/PHPjc/752330.html techarticle PHP 5.4 was settled this month, and it was the first major update to PHP since 2009. The language section has been enhanced to include support for Traits and removal of some of the controversial features. Tra ...

  • Related Article

    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.