[Reprint]php Array Class object value pass Reference pass Difference

Source: Internet
Author: User

The General data type (int, float, bool) does not explain this.

Here is a detailed description of the difference between an array and the object of a class as a parameter for value passing

Array value passing

Instance code:

<?php function Main () {        $cc = array (            ' A ', ' B '        );        Change ($CC);        Var_dump ($CC);        Die;} function Change ($CC) {        $cc = array (' DD ');} Main ();? >

  output:

  array(2) {      [0]=>      string (1) "a"      [1]=>      string (1) "b"   }

Array reference passing

<?php function Main () {        $cc = array (            ' A ', ' B '        );        Change ($CC);        Var_dump ($CC);        Die;} Function Change (& $CC) {        $cc = array (' DD ');} Main ();? >
Outpout:array (1) {  [0]=>  string (2) "DD"}

Class Object Value passing

<?phpclass pp{public        $ss = 0;} function Main () {        $p = new pp ();        Change ($p);        Var_dump ($p);        Die;} function Change ($p) {        $p->ss = 10;} Main ();? >
Output:object (PP) #1 (1) {  ["ss"]=>  Int (10)}

Class object reference passing

<?phpclass pp{public        $ss = 0;} function Main () {        $p = new pp ();        Change ($p);        Var_dump ($p);        Die;} Function Change (& $p) {        $p->ss = 10;} Main ();? >
Object (PP) #1 (1) {  ["ss"]=>  Int (10)}

Summary: In PHP, an array is a normal variable, and the value is passed as a copy of the argument, regardless of the argument, and the value of the argument can be changed after the reference is passed.
The object of a class is that the value of the argument is changed regardless of whether the value is passed or the reference pass is a reference pass, and is a reference to the object.

Transferred from: http://www.cnblogs.com/zcy_soft/archive/2011/12/10/2283570.html

[Reprint]php Array Class object value passing reference passing differences

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.