Sunday, April 30, 2017
Tags Posts tagged with "new in php7"

new in php7

by -
0 995
New in PHP 7
New in PHP 7

To date, PHP has released the official version php7 RC7, the distance to release the first official version will not be far away! For now php7 major feature must have been finalized, and will not have any change of. Some versions of the subsequent iteration is primarily trim bug, optimization and the like. Here to talk to those who have a major change php7 we have been looking forward to the. . .

About PHP

 

20-year history;

By far the most popular WEB development language;

More than 82% of sites are using PHP as their service-side development language;

New Features Introduction

 

  • PHP NG – Zend Engine 3
  • Abstract syntax tree
  • The INT 64 support
  • Unified variable syntax
  • Added Closure :: call ()
  • Consistency foreach loop
  • Support anonymous class
  • Added <=> , ** , ?? , \ u {xxxx} operator
  • Increase the return type of declaration
  • Increasing the scalar type of declaration
  • Core error by exception catching
  • Adds context-sensitive lexical analysis

 

PHP NG

  • ZVAL
  • Size reduced from 24 bytes to 16 bytes
  • Zend Array (HashTable)
  • HashTable size reduced from 72 bytes to 56 bytes
  • HashTable bucket size was reduced from 72 bytes to 32 bytes
  • Optimization function calls
  • Use the new memory allocation, management, reducing waste of memory
  • Immutable array optimization

 

$ Arr = [];

 

for ( $ i = 0 ; $ i < 100,000 ; $ i ++) {

$ arr [] = [ ‘PHP’ ];

}

 

P (memory_get_usage ( true ));

 

PHP5: 45M

PHP7: 10M

  • Some very common and low-cost function directly into the engine support of opcode

call_user_function (_array) => ZEND_INIT_USER_CALL

is_int , is_string , is_array , … => ZEND_TYPE_CHECK

strlen => ZEND_STRLEN

defined => ZEND + DEFINED

 

  • Optimizing core sorting

PHP5 ( zend_qsort )

quicksort (non-stable sort)

 

array ( 1 => 0 , 0 => 0 )

PHP7 ( zend_sort )

selection sort quick sort + (stable sort)

 

array ( 0 => 0 , 1 => 0 )

Less than 16 elements using selection sort, more than 16 units to 16 in accordance with the division, respectively, using selection sort, and then all together using the quick sort. Sort the more than before, the internal elements of a non-stable sort becomes stable sort, reducing the number of switching elements, reducing the number of operations on memory performance improvement of 40%

Abstract syntax tree

New in Php 7
New in Php 7

 

If now we have such a demand, to the source files for php-line syntax testing, to achieve coding standard. php5 before then, there is no AST, directly from the parser generates the opcodes! We need the help of some external php parser to achieve; and php7 increased AST, we can go to achieve such an extension, using the extended functions provided can directly access files corresponding to the AST structure, and this structure is what we It can be identified, so we can do some optimization and a judge on this basis.

The INT 64 support

 

  • Support store more than 2GB of string
  • Support upload a file size larger than 2GB
  • Guarantee strings on all platforms [64] are 64bit

Uniform syntax variables

$$ Foo [ ‘Bar’ ] [ ‘baz’ ]

PHP5: $ {$ foo [‘Bar’] [‘baz’]}

PHP7: ($$ foo) [‘Bar’] [‘baz’] [rule] from left to right

( function ()  {}) ();

$ Foo () ();

select allcopyPut notes[ $ obj, ‘method’ ] ();

 

class A {

public static function a1 () {}

}

 

[ A new, ‘A1’ ] ();

Added Closure :: call ()

 

$ F = function ()  {

P ( $ this -> name);

};

 

class  F  {

Private  $ name = ‘F’ ;

}

 

$ F -> Call ( new F);

Support anonymous class

 

function  getAnonymousClass ($ config)  {

return  new  class (config) {};

}

 

p (getAnonymousClass (array ()));

Consistency foreach loop

// PHP5

$ a = array ( 1 , 2 , 3 ); foreach ( $ a as $ v ) {var_dump (current ( $ a ));}

int ( 2 )

int ( 2 )

int ( 2 )

 

$ A = array ( 1 , 2 , 3 ); $ b = & $ a ; foreach ( $ a as $ v ) {var_dump (current ( $ a ));}

int ( 2 )

int ( 3 )

bool (false)

 

$ A = array ( 1 , 2 , 3 ); $ b = $ ​​a ; foreach ( $ a as $ v ) {var_dump (current ( $ a ));}

int ( 1 )

int ( 1 )

int ( 1 )

 

// PHP7: no longer operating data of the internal pointer

$ A = array ( 1 , 2 , 3 ); foreach ( $ a as $ v ) {var_dump (current ( $ a ))}

int ( 1 )

int ( 1 )

int ( 1 )

 

$ A = array ( 1 , 2 , 3 ); $ b = & $ a ; foreach ( $ a as $ v ) {var_dump (current ( $ a ))

int ( 1 )

int ( 1 )

int ( 1 )

 

$ A = array ( 1 , 2 , 3 ); $ b = $ ​​a ; foreach ( $ a as $ v ) {var_dump (current ( $ a ))}

int ( 1 )

int ( 1 )

int ( 1 )

Several new operators

 

<=>

 

select allcopyPut notes// PHP5

function  Compare ( $ a , $ b )  {

return ( $ a < $ b ) -? 1 : (( $ a > $ b )? 1 : 0 );

}

// PHP7

function  Compare ( $ a , $ b )  {

return  $ a <=> $ b ;

}

**

 

2 ** 2 ; // 2 * 2 = 4

2 ** – 1 ; // 1/2 = 0.5

3 ** – 2 ; // 1/9 = 0.111111111

??

 

$ A = null ;

$ b = 1 ;

$ c = 2 ;

echo  $ a ?? $ b , ‘,’, $ c ?? $ b ; // 1,2

echo  $ a ?? $ b ?? $ c   , ‘,’, $ a ?? $ d ?? $ c ; // 1,2

\ U {xxxx}

 

echo  “\ u {4f60}” ; // you

echo  “\ u {65b0}” ; // New

// from right to left to force

echo “\ u {202E} iabgnay \ u {1F602}” ;;

😂 yangbai

The return type of declaration

 

function  getInt () : int  {

return  ‘Test’ ;

};

 

getInt ();

 

// Function return value DateTime

function  getDateTime () : DateTime  {

return  new DateTime ();

};

Scalar type of declaration

function  getAmount (int $ num ) : int  {

return  $ num ;

};

 

getAmount ( ‘Test’ );

 

// PHP5

#PHP Catchable fatal error: Argument 1 passed to getInt () MUST be an instance of int, String Given …

 

// PHP7

#Fatal error: Uncaught TypeError: Argument 1 passed to getInt () MUST be of the type Integer, String Given …

 

getAmount ( ‘123’ );

# PHP7 new strict mode option will open the next error [declare (strict_types = 1), pay attention to put the first line of code]

Core error can be caught by an exception

try {

non_exists_func ();

} catch (EngineException $ e ) {

echo  “Exception: {$ e> getMessage ();} \ N” ;

} finally {

echo  “undefined function …” ;

}

 

// Here with php7 tried it but it does not capture the success [] throws an exception. . .

#Exception: Call to undefined function non_exists_func ()

Up and down asking sensitive lexical analysis

 

select allcopyPut notes// PHP5

class  Collection  { public  function  foreach ( $ arr )  {}}

#parse error: parse error, expecting `” identifier (T_STRING) “‘…

 

// PHP7

class  Collection  {

public  function  foreach ( $ arr )  {

return  $ this ;

}

public  function  in ( $ arr ) {

return  $ this ;

}

public  function  Sort ( $ condition ) {

return  $ this ;

}

public  function  echo ( $ condition ) {

return  ‘OK’ ;

}

}

$ Collection = new Collection ();

$ Collection -> in () -> foreach () -> Sort () -> echo ();

Break something

  • mysql, ereg

mysql moved ext / pecl go up, ereg moved ext / pcre

  • isapi, tux etc SAPIs
  • <? And <? Language = “php” Such a label is removed
  • HTTP_RAW_POST_DATA removed (you can use php: // input substitution)
  • $ O = & new className () , no longer supports such wording
  • mktime () , gmmktime () function $ is_dst parameters are removed
  • setlocale () function in the $ category parameter does not support the string, and must be a constant LC beginning
  • ini file to remove the # as a comment, unified use; to comment
  • Function definition with the same name parameter is not supported
  • The same name as the class constructor is not recommended (currently not removed, follow-up will be removed)
  • String , int , float , etc. These keywords can not be used as classes, interfaces, trait name used
  • func_get_arg / func_get_args get is the value of the current variable
  • Invalid octal number will produce a compilation error
  • preg_replace () no longer supports a matching pattern / e
  • Hexadecimal strings to digital converter have been removed
  • No longer supports static call an incompatible $ this context of non-static calls
  • Unsafe curl file uploads (use CurlFile instead)

// PHP5

curl_setopt (ch, CURLOPT_POSTFIELDS, array (

‘File’ => ‘@’ .realpath ( ‘image.png’ ),

));

 

// PHP7

curl_setopt (ch, CURLOPT_POSTFIELDS, [

‘File’ => new CURLFile (realpath ( ‘image.png’ )),

]);

Some functions and options removed

set_magic_quotes_runtime ();

magic_quotes_runtime ();

 

// ( Use stream_set_blocking () instead)

set_socket_blocking ();

 

// ( Use mcrypt_generic_deinit () instead)

mcrypt_generic_end ();

 

// ( Use mcrypt_encrypt () and mcrypt_decrypt () instead)

mcrypt_ecb ();

mcrypt_cbc ();

mcrypt_cfb ();

mcrypt_ofb ();

 

// ( Use datefmt_set_timezone () or IntlDateFormatter :: setTimeZone () instead)

datefmt_set_timezone_id ();

IntlDateFormatter :: setTimeZoneID ();

 

// ( Use XSLTProcessor :: setSecurityPrefs () instead)

xsl.security_prefs; //php.ini

 

// ( Use php.input_encoding, php.internal_encoding and php.output_encoding instead)

iconv.input_encoding;

iconv.output_encoding;

iconv.internal_encoding;

mbstring.http_input;

mbstring.http_output;

mbstring.internal_encoding;

 

( Use PDO :: ATTR_EMULATE_PREPARES instead)

PDO :: PGSQL_ATTR_DISABLE_NATIVE_PREPARED_STATEMENT; // Driver option

 

// ( Use PEER_NAME instead)

CN_match; // SSL context Options

SNI_server_name; // SSL context options