#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    3
    Rep Power
    0

    PHP 5.4 removing call-time reference for non-object vars?


    I can understand deprecating the ampersand (&) for call-time references for objects (like function foo(&$object)) but not for strings etc. This seems useful so I don't have to do this:

    $string = foo($string);

    It seems like foo(&$string) should be perfectly acceptable and not cause problems or be unnecessary.

    Am I missing something here?
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,936
    Rep Power
    1045
    Hi,

    this is a misunderstanding. Passing by reference works like it always did, but you have to declare the reference in the function definition, not the function call:
    PHP Code:
    <?php

    function f(&$x) {
        
    $x[0] = 'x';
    }

    $str 'abc';
    f($str);        // no, not f(&$str)        
    echo $str;
    Last edited by Jacques1; December 5th, 2012 at 03:12 PM.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    11
    Rep Power
    0
    Well, you could say in a readability point of view output parameters are quite misleading, and $string = foo($string); is the better approach. But that's just my opinion (and the opinion of some design guru's)

IMN logo majestic logo threadwatch logo seochat tools logo