Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Jun 2010
    Posts
    293
    Thanks
    63
    Thanked 8 Times in 8 Posts

    difference between value and reference in function calls with object arguments?

    Consider the following code:

    PHP Code:
    class myclass
    {
      public 
    $value 0;
    }

    $mc = new myclass;

    function 
    passByValue(myclass $cls)
    {
      
    $cls->value++;
    }

    function 
    passByReference(myclass$r_cls)
    {
      
    $r_cls->value++;
    }

    passByValue($mc);
    passByReference($mc);

    echo 
    $mc->value."\n"
    This code will output 2, as you would expect.

    It seems to me that passByValue and passByReference are identical because objects are apparently passed by reference anyway. But ... is there actually any difference that I'm not aware of?
    Last edited by XmisterIS; 01-04-2012 at 11:38 AM.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    No, there is a tremendous difference between pass-by-value, and pass-by-reference. When it comes to Objects in PHP5+, no reference is required. PHP uses a link identifier to access its objects, so it is still pass-by-reference and pass-by-value, the sole difference is another level of indirection. The end result is still the same identifier. This is a simple signature difference:
    PHP Code:
    void passByValue(myclass *cls);
    void passByReference(myclass **cls); 
    But we don't need to deal with the indirection level in PHP.

    You can only see this difference when it comes to direct assignments:
    PHP Code:
    <?php
    class myclass
    {
      public 
    $value 0;
    }
    $aHash = array();
    $mc = new myclass;

    function 
    passByValue(myclass $cls)
    {
        
    $cls = new myclass();
        
    $cls->value __FUNCTION__;
        return 
    $cls;
    }

    function 
    passByReference(myclass$cls)
    {
        
    $cls = new myclass();
        
    $cls->value __FUNCTION__;
        return 
    $cls;
    }

    $obj passByValue($mc);
    //$obj2 = passByReference($mc);

    echo $mc->valuePHP_EOL;  
    print 
    $obj->value PHP_EOL;
    print 
    $obj2->value PHP_EOL;
    $mc will have a value of 0. $obj will have a value of 'passByValue'. If you uncomment the $obj2 call, $mc and $obj2 will have a value of 'passByReference' and $obj will have a value of 'passByValue'.

  • Users who have thanked Fou-Lu for this post:

    XmisterIS (01-04-2012)

  • #3
    Regular Coder
    Join Date
    Jun 2010
    Posts
    293
    Thanks
    63
    Thanked 8 Times in 8 Posts
    I gotcha! Seeing the two C-style signatures at the top makes it make sense!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •