New Features of PHP5.5

Source: Internet
Author: User
Tags exit in html generator
Www.cnblogs.comyjf512p00004400.html generator (Generators) generator is one of the most anticipated new features. It allows developers to implement traversal without implementing the iterator interface. To compile a class that implements the iterator interface, you need to copy a lot of duplicate code. Now you can use the generator to reduce the amount of code and

The http://www.cnblogs.com/yjf512/p/3164400.html generator (Generators) generator is one of the most promising new features, which enables developers to achieve traversal without implementing the iterator interface. To compile a class that implements the iterator interface, you need to copy a lot of duplicate code. Now you can use the generator to reduce the amount of code and

Http://www.cnblogs.com/yjf512/p/3164400.html

Generators)

The generator is one of the most promising new features. It allows developers to implement traversal without implementing the iterator interface. To compile a class that implements the iterator interface, you need to copy a lot of duplicate code. Now you can use the generator to reduce the amount of code and complexity.

The generator is implemented using the newly added keyword yield. Its shape is similar to that of a common function, but unlike the function that only returns a single value, the generator can generate any value. The following is an example of its powerful functions. Consider the range () function in PHP, which returns a numerical array between $ start and $ end, as follows:

 

In this example, the array returned by the range () function will occupy a large amount of memory (about 100 Mb). In this simple example, large arrays are often created in real programs, they consume a lot of time and memory. After the generator is introduced, we can solve this problem without writing the iterator class. The generator does not create a large array, but returns a value during each iteration. The above example can be changed to the generator version:

  

The running result of this Code is the same as that of the first example. However, during the running process, no large array is created to save all values, so less than 1 kb of memory is required, memory consumption is greatly reduced than the original code.

Password hash

The new password hash API is a very important and practical feature in php5.5. In the past, developers had to rely only on other crypt () functions, and these functions were not fully documented, resulting in many misuse. The new API is simple and clear, allowing developers to implement secure password hashing.

The new API contains password_hash () and password_verify () functions, and CALLS password_hash ($ password, PASSWORD_DEFAULT) to return a hash value encrypted with bcrypt and automatically adds the salting value. When the password is verified, password_verify ($ password, $ hash) is called ). The API uses bcrypt by default, and other new and safer encryption methods may be introduced in the future. Developers can adjust the bcrypt parameter to increase the encryption strength, and specify the salt value by themselves (but this is not recommended officially ).

Finally

PHP5.5 supports the finally keywords commonly used in exception handling in other languages. developers can run the Specified Code after try and catch blocks without worrying about whether exceptions are thrown, then return to the normal execution flow. Before that, the developer can only copy the code in the try and catch blocks to complete related task cleaning. For example, in the following example, releaseResource () must be called in two places ():

   getMessage());        exit();    }    releaseResource($resource);    return $result;}

  

With the finally keyword, you can delete the redundant code:

   getMessage());        exit();    }    finally {        releaseResource($resource);    }}

For the modified Code, we only need to call the releaseResource () Cleaning function in the finally block. No matter whether the process finally goes to the return Statement in the try or the exit in the catch, the finally code will be executed.

Array and string literal expression

Currently, the syntax for accessing arrays supports the unreferencing of arrays and strings:

   

This feature mainly enhances language consistency, which may not affect the code writing behavior at ordinary times, but it is very convenient to use in some scenarios:

    

Empty () supports function calls and expressions.

Empty () is a language structure that can be used in function calls and expressions, such as empty ($ object-> getProperty ()). In this way, you can use empty () to determine the function return value, instead of assigning the return value to a temporary variable, and then use empty () for the temporary variable ().

Class name resolution

After the namespace is introduced from PHP5.3, it is common to use it to organize the class structure in PHP projects. However, it is very difficult to retrieve the fully qualified class names with namespaces, such:

     

This code will fail because it will search for the class Foo from the global namespace instead of the specified namespace. PHP5.5 introduces the class keyword, which can be used to obtain the fully qualified class name:

      

In the above Code, Foo: class is parsed as "Namespaced \ Class \ Foo ".

Foreach Improvement

The list () language structure can easily assign values in the array to a group of variables, such:

       

Now you can use list () to traverse multi-dimensional arrays using foreach ():

        

This feature makes the traversal of nested arrays easier and more concise, and the foreach loop starts to support non-standard values as the iterator key, that is, the key of an element can be other types of values other than strings and integers.

Conclusion

PHP5.5 provides many improvements for efficient PHP development. In addition to these new features, it also modifies a large number of bugs. For more information, see ChangeLog.

Http://phpmaster.com/whats-new-in-php-5-5/

 

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.