Analysis of the definition of array attribute in PHP 5.6 incompatible with Previous version

Source: Internet
Author: User
Several cases of incompatibility with previous versions are mentioned in the incompatible page (http://php.net/manual/zh/migration56.incompatible.php) of the official php5.6 document, which mentions the incompatibility when defining array properties for a class, with the original text and examples as follows:

In versions prior to PHP 5.6, when defining the properties of an array type for a class, if both an explicit array key and an implicit array key are used in the array, and the explicit key is the same as the implicit sequence key, the key of the array is overwritten. For example:

1 
 
   ' foo ', 6         ' Bar ', 7         ' Quux ', 8     ]; 9}10 var_dump ((new C)->array);?>

The above example outputs in PHP 5.5:

1 Array (2) {2   [0]=>3   string (3) "Bar" 4   [1]=>5   string (4) "Quux" 6}

Output in PHP 5.6:

Array (3) {  [1]=>  string (3) "foo"  [2]=>  string (3) "Bar"  [3]=>  string (4) "Quux "}

The above example, in fact, is a very special case, the premise of its appearance is a few:

1. The array property must be defined for the class and is the case where the property is initialized when the class variable is defined;

2. It must be an explicit array key before the implicit array key in the case of an implicit array key to the value of the display array key with the same position as itself; If the implicit array key is in front of the explicit array key, then the overwrite is bound to occur, regardless of the version;

3. An explicit array key must be a constant (including class constants), and if it is a literal constant, no implicit array key overrides the explicit array key, regardless of the version;

Examine the output of the following example in PHP 5.5:

1.1 Non-class array member definitions:

1 $array = [2 one=> ' foo ', 3  ' Bar ', 4  ' Quux ' 5];6 var_dump ($array);

Output:

Array (3) {  [1] = =  string (3) "foo"  [2] =  string (3) "Bar"  [3] = = String  (4) "Quux"}

1.2 Assignment at non-initialization:

1 define (' one ', 1); 2 class C {3 public $array; 4}5 $c = new c;6 $c->array = [one=> ' foo ', ' Bar ', ' Quux '];7 var_dump ($c- >array);

Output:

Array (3) {  [1] = =  string (3) "foo"  [2] =  string (3) "Bar"  [3] = = String  (4) "Quux"}

1 define (' one ', 1); 2 class C {3 Const one = 1; 4 public  $array; 5 public  function __construct () {6 $this->array = array (7 ' bar ' , 8 one =  ' foo ', 9  ' Quux ', ten  ), one  }12}13 $c = new c;14 var_dump ($c->array);

Output:

Array (3) {  [0] = =  string (3) "Bar"  [1] = =  string (3) "foo"  [2] = =  string (4) " Quux "}

2. Explicit key overrides for an implicit key:

1 define (' one ', 1); 2 class C {3 Const one = 1; 4 public  $array = Array (5 ' bar ', 6  ' Quux ', 7 one =  ' foo ', 8  ' guru ' 9
  

Output:

Array (3) {  [0] = =  string (3) "Bar"  [1] =  string (3) "foo"  [2] = =  string (4) "Guru"}

3. Literal constants as explicit key names:

1 class C {2 Public $array = Array (3 ' bar ', 4  ' Quux ', 5  1 = ' foo ', 6  ' guru ' 7  ); 8} 9 $c = new C;10 Var_dump ((new C)->array);

Output:

Array (3) {  [0] = =  string (3) "Bar"  [1] = =  string (3) "foo"  [2] = =  string (4) " Guru "}

1 class C {2 Public $array = Array (3 1 = ' foo ', 4  ' Bar ', 5  ' Quux ', 6  ); 7}8 $c = new C;9 var_dump ((new C)-> ; array);

Output:

Array (3) {  [1] = =  string (3) "foo"  [2] =  string (3) "Bar"  [3] = = String  (4) "Quux"}

In fact, this is a very deep hidden small bug.

In the definition of an array, if a partial cell specifies a numeric key name, the following implicit key name continues the previous maximum number (including implicit or explicit) key names, and should not result in an implicit key name overriding the explicit numeric key name, regardless of whether the explicit key name is a literal constant or a symbolic constant.

See the Official Handbook Array section: http://php.net/manual/zh/language.types.array.php:

1 
 
   "C", 6          "D", 7); 8 Var_dump ($array); 9?>

The above routines will output:

Array (4) {  [0]=>  string (1) "A"  [1]=>  string (1) "B"  [6]=>  string (1) "C"  [7]= >  string (1) "D"}

However, the previous version of 5.6, when implemented as a class to initialize an array member, uses symbolic constants as numeric keys to name some of the cells, and there are cases where the definition of the array semantics does not match.

Since such a situation is extremely rare, the impact is small, but once it does, the resulting bug is extremely difficult to find.

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