Precautions for PhP5 foreach object Array

Source: Internet
Author: User

Submitted by marsmus on 2009, June 9, PM. php

Let's take a look at a piece of code to introduce problems from this code. Read the following sample code carefully to see if there are any problems (I didn't know how to process the PhP5 foreach object array before, so I thought it was the same as a normal array, and it was a copy operation)

<? Php <br/>/** <br/> * first write a simple class, class for storing HTML control data <br/> */<br/> class element {<br/>/** <br/> * only for demonstration, simplify all processes <br/> */<br/> protected $ name; <br/> protected $ value; </P> <p> public function _ construct ($ name, $ value) {<br/> $ this-> name = $ name; <br/> $ this-> value = $ value; <br/>}</P> <p> Public Function setname ($ name) {<br/> $ this-> name = $ name; <br/>}</P> <p> $ element_list = array (); <br/> Fo R ($ I = 0; $ I <2; $ I ++) {// generate an array with two element objects <br/> $ element_list [] = new element ('name _'. $ I, 'value _'. $ I); <br/>}</P> <p> // Print $ element_list first, view $ element_list current data <br/> echo '-------- old $ element_list start --------'; <br/> print_r ($ element_list ); <br/> echo '-------- old $ element_list end --------'; </P> <p>/** <br/> * use foreach to process $ element_list elements. <br/> * after processing, $ element_list should be the same as the original element and remain unchanged <br/> * because Each does not affect the value of the original array <br/> */<br/> foreach ($ element_list as $ key => $ element) {<br/> $ element-> setname ('changed '); <br/>}</P> <p> // Print $ element_list to check its current value. <br/> echo '-------- new $ element_list start --------'; <br/> print_r ($ element_list); <br/> echo '-------- new $ element_list end --------'; <br/>/* The following is the output result <br/> -------- old $ element_list start -------- <br/> array <br/> (<br/> [0] => element object <br /> (<Br/> [name: protected] => name_0 <br/> [value: protected] => value_0 <br/>) </P> <p> [1] => element object <br/> (<br/> [name: protected] => name_1 <br/> [value: protected] => value_1 <br/>) </P> <p>) <br/> -------- old $ element_list end --------- </P> <p> ------- new $ element_list start -------- <br/> array <br/> (<br/> [0] => element object <br/> (<br/> [Name: protected] => changed <br/> [value: prote CTED] => value_0 <br/>) </P> <p> [1] => element object <br/> (<br/> [Name: protected] => changed <br/> [value: protected] => value_1 <br/>) </P> <p>) <br/> -------- new $ element_list end -------- <br/> */<br/>?>

 

From the output, we can see that the data of the object in $ element_list has changed. This is similar to the previous introduction of using foreach to traverse the array elements, processing elements does not affect the conflicting values of the original array. Is there a bug in PHP? No. The reason for this is that PhP5 uses references for all objects, which is completely different from PhP4, when creating a new object, PhP4 uses the & operator to indicate that the object is referenced.

Since it is a reference, the $ element_list array stores only the reference of the element object rather than copying an object. Then we return to foreach. When foreach processes an array, it copies the original array and then extracts each element from it. We use foreach to process these elements, instead of processing the original array, it is only processing a copy, so no matter how we change its value, the value of the original array will not change (this is only useful for common variables, and objects in PhP5 will not apply ). If the current array is an object array, the value of PhP5 is the reference of the object. When we use foreach to process it, this is the case, as shown below:

1. First copy a $ element_list, which is called $ element_list_copy. In foreach processing, all processing operations are performed on the $ element_list_copy array, without affecting the original array $ element_list.

2. get an element in $ element_list_copy and process it. In this case, the element in $ element_list_copy is also a reference to the $ element object, which has the same value as the value in $ element_list, they are all references to a $ element object. The two objects point to the same memory space. Therefore, they are used to process the $ element_list_copy element and $ element_list element, the operated data blocks are the same, that is, the elements in $ element_list_copy are changed, and the elements in $ element_list are changed accordingly, because the $ element_list_copy and $ element_list elements point to the same memory, they are all the same $ Element Object.

This is why the data of the object in the $ element_list object array has changed after foreach processing. Pay attention to the differences between object arrays and general arrays in PhP5 using foreach. Otherwise, it is very likely that unexpected errors will occur.

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.