PHP Object array/pass array parameter

Source: Internet
Author: User

In PHP, the normal data type of the parameter pass is assigned by copy, and the object object will pass the reference to the objects, in the function call or direct assignment will follow this rule, and in PHP, the array () is passed to the ordinary data type (with our is passed when the array is passed, the array cannot be copied using clone, but the entire array will be copied when it is assigned directly, but whether the array will completely copy the contents and determine according to the item contents of the array: when the array item is a normal data type (value, An array of etc.), which will directly copy the entire array including the contents, when the array's item is an object, only the reference to the array is copied, and the content of the object pointed to is not copied. So the object array needs to use clone on each item to replicate to the entire array.

1 classT {2 3      Public $curarray;//Class Array member4 5     functionT$value) {6         $this->curarray =$value;7     }8 9     functionChange$cas) {//Changing the contents of an arrayTen          for($j= 0;$j< 10;$j++) { One             $this->curarray[$j] =$cas; A         } -     } -  the     functionGet () {//Returns an array -         return $this-Curarray; -     } -  + } -  + classNode { A  at      Public $val; -  -     functionNode ($v) { -         $this->val =$v; -     } -  in } -  to functionTest () { +     $array 1= []; -      for($i= 0;$i< 10;$i++) { the         Array_push($array 1, 0); *     } $     $obj 1=NewT$array 1);Panax Notoginseng     $obj 1->change (2); -     //the item content of the $array 1 is a normal type, so the parameter is the entire copy parameter, $array 1 of the content will not change the  +     $array 2= []; A      for($i= 0;$i< 10;$i++) { the         Array_push($array 2,NewNode (0)); +     } -     $obj 2=NewT$array 2); $     $obj 2->change (2); $     //The item content of the $array 2 is object, so the equivalent of a reference array is passed, so modifying the contents of the array causes the content of the $array2 to change -  -     $tmpArray= []; the     foreach($array 2  as $item) { -         Array_push($tmpArray,Clone $item);Wuyi     } the     //Using this clone method is to copy the incoming parameters, the change method does not affect the content of $array2 -}

PHP Object array/pass array parameter

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.