Talk about PHP7 new function _php instance

Source: Internet
Author: User
Tags closure generator php language rfc

PHP7 has been released for half a month, and recently had time to understand the new features of PHP7, of course, the biggest feature of this version is performance improvement. In the next is not a master, you are welcome to point out mistakes, and look forward to common exchanges.

A very important feature of the PHP language is "weak type", which makes PHP programs very easy to write, novice access to PHP can quickly start, but it is also accompanied by some controversy. Support for the definition of variable types, which can be said to be a change in nature, PHP began to support the type definition in an optional way. In addition, a switch instruction declare (strict_type=1) is introduced, and when this instruction is turned on, the program under the current file will be forced to follow the strict function parameter type and return type.

1. Increased group support in use statement

Use foolibrary\bar\baz\{ClassA, CLASSB, CLASSC, CLASSD as Fizbo};

2. Increase?? Operator

Isset ($_get[' MyKey ')? $_get[' MyKey ']: "" Bulky
$_get[' MyKey ']?: "" When MyKey does not exist, it will report a e_notice $_get[
' MyKey ']?? DefaultValue ' security will not be reported e_notice
$username = $_get[' user ']?? ' Nobody ';

3.64-bit PHP7 string length can exceed 2^31 bytes.

4. Increase Closure::call Support

Closure::call dynamically binds a closure function to a new object instance and invokes the execution of the function.

 <?php
 class Value {
 protected $value;
 Public function __construct ($value) {
 $this->value = $value;
 }
 Public Function GetValue () {return
 $this->value;
 }
 }
 $three = new Value ();
 $four = new Value ();
 $closure = function ($delta) {var_dump ($this->getvalue () + $delta);
 $closure->call ($three,);
 $closure->call ($four,);
 ? >
 

5. Double quote strings and Heredocs inside support use \U{XXXXX} to declare Unicode characters.

6.define support for pairs of arrays

 Define (' ANIMALS ', Array (
 ' dog ',
 ' cat ',
 ' bird '
 ));
 

7. Increase the comparison operator <=>

$a <=> $b
If a equals B is 0
1 if a is greater than B
If a is less than B is-1

8.php global reserved words can be declared using the

 Class View {public
  function include (View $view) {
   //...
  }
 

Include keywords can be used as normal string keywords

9. Scalar type (Int,float,string,bool) support

Increase the Declare (strict_types=1) Directive declaration for strict type validation.
When declaring declare in the file header (Strict_types=1)

 <php
 Declare (strict_types=);
 function add (float $a, float $b): float {return
  $a + $b;
 }
 

If the above code does not open declare (Strict_types=1) perhaps declare (strict_types=0), PHP will automatically convert parameters and return values to the specified type.
Open declare (Strict_types=1) and throw an error if the parameter type is not Flaot and perhaps the return type is not float

10. Increase the interface to provide a safe and convenient random number generator for the user layer. RFC: https://wiki.php.net/rfc/easy_userland_csprng (further discussion)

11. Added the yield from operator. https://wiki.php.net/rfc/generator-delegation (further discussion)

Knowledge is what we know and what we are not aware of. Based on existing knowledge, we go to discover the unknown. The more knowledge we acquire, the more we know the knowledge will be.

Attached: PHP7 the principle behind the outstanding performance

PHP7 does not change in principle compared with PHP5, which is different from HHVM. It is mainly based on the Perf Performance analysis tool to perform the general performance optimization.

Reduce memory allocation times, use stack memory, cache array hash value, string resolution to macro expansion, the use of large chunks of contiguous memory in place of small block of memory and so on. For more details, please see Brother Bird's PHP7 share ppt. Zend Engine program performance improved a lot, PHP program performance naturally good. All the optimizations are real and add up to the details.

Figuring out how to optimize a 20-year history program is a very challenging thing to do, and it's more difficult than a refactoring. 80% of the world's Web site with PHP, the use of PHP7 after these web programs without too much cost can be 1 time times the performance of the upgrade free. How much CPU is saved, how much electricity is saved, and how much carbon is emitted. In addition PHP is open source project, there is no commercial company behind, completely by community operation. Developers and businesses using the PHP language have never paid a penny to the PHP authorities. So I suggest you know friends, no matter how much you despise PHP, but please respect others for the world to pay.

The above content is a small series to introduce PHP7 new features and PHP7 excellent performance behind the principle, I hope you like.

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.