Professional Web Applications Themes

Which is better, pass, or not pass by reference? - PHP Development

Folks, I've asked this before but never got any response but its important and I thought I'd pitch it again (hopefully a bit clearer)... One can pass data from one function to another either by passing a copy, or passing by reference. My understanding of passing by reference (putting the & before a variable during the function declaration) means that the original data is used. My understanding of *not* passing by reference, (thus passing a copy) literally means creating a copy of the data contained in my variables - thus doubling the memory usage for this data... even if its ...

  1. #1

    Default Which is better, pass, or not pass by reference?


    Folks,

    I've asked this before but never got any response but its important and I
    thought I'd pitch it again (hopefully a bit clearer)...

    One can pass data from one function to another either by passing a copy, or
    passing by reference.

    My understanding of passing by reference (putting the & before a variable
    during the function declaration) means that the original data is used.

    My understanding of *not* passing by reference, (thus passing a copy)
    literally
    means creating a copy of the data contained in my variables - thus doubling
    the memory usage for this data... even if its just a temporary while the
    function is being executed.

    I *believed* (past tense) that one should only pass by reference if they
    wanted the child function to change the data and pass the newer values back
    to the parent function.

    I'm now thinking that this is just a feature and not the only usage. I am
    begining to think I should use "pass by reference" nearly all the time in
    order to conserve memory (since I won't be creating copies of my variable
    data in memory).

    Would this be good practice? Am I right that passing by reference means that
    my variable data is not duplicated thus using less memory than if I had
    passed my variable data as a copy?
    --
    All comments + replies please via the newsgroup,
    Thanks,
    Randell D.
    -
    A: Because it messes up the order in which people normally read text.
    Q: Why is top-posting such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet?


    Randell D. Guest

  2. #2

    Default Re: Which is better, pass, or not pass by reference?

    "Randell D." <you.can.email.merandelld.at.> wrote in message
    news:cU4lb.138072$6C4.80562pd7tw1no...
    > I'm now thinking that this is just a feature and not the only usage. I am
    > begining to think I should use "pass by reference" nearly all the time in
    > order to conserve memory (since I won't be creating copies of my variable
    > data in memory).
    _if_ I am correct, php 5 will pass by reference by default.

    rush
    --
    [url]http://www.templatetamer.com/[/url]



    rush Guest

  3. #3

    Default Re: Which is better, pass, or not pass by reference?


    "Randell D." <you.can.email.merandelld.at.> schreef in bericht
    news:cU4lb.138072$6C4.80562pd7tw1no...
    >
    > Folks,
    >
    > I've asked this before but never got any response but its important and I
    > thought I'd pitch it again (hopefully a bit clearer)...
    >
    > One can pass data from one function to another either by passing a copy,
    or
    > passing by reference.
    >
    > My understanding of passing by reference (putting the & before a variable
    > during the function declaration) means that the original data is used.
    >
    > My understanding of *not* passing by reference, (thus passing a copy)
    > literally
    > means creating a copy of the data contained in my variables - thus
    doubling
    > the memory usage for this data... even if its just a temporary while the
    > function is being executed.
    >
    > I *believed* (past tense) that one should only pass by reference if they
    > wanted the child function to change the data and pass the newer values
    back
    > to the parent function.
    >
    > I'm now thinking that this is just a feature and not the only usage. I am
    > begining to think I should use "pass by reference" nearly all the time in
    > order to conserve memory (since I won't be creating copies of my variable
    > data in memory).
    >
    > Would this be good practice? Am I right that passing by reference means
    that
    > my variable data is not duplicated thus using less memory than if I had
    > passed my variable data as a copy?
    > --
    > All comments + replies please via the newsgroup,
    > Thanks,
    > Randell D.

    Passing by reference is usefull if you want a function to return more than
    only one value... a function returns normally only one value (although you
    can return an array with multiple results too)...

    Using a reference is done if you want to change a var outside the scope of
    your function....

    Yes, interesting, why not always use call by reference??? Only thing i can
    think is that you do not always want to change values outside the
    function...

    Regards,

    Marcel


    Marcel Guest

  4. #4

    Default Re: Which is better, pass, or not pass by reference?

    Reference is useful only for object.

    if your code is object oriented, then you should always return
    references to your objects, never copies, or you will have some
    surprises (change the object in one place, without changing it somewhere
    else is bad)

    for built-in types like strings, integers, booleans , return copies, you
    will double the use of memory but for this kind of elements, it has
    absolutly NO importance (I'm not even sure you can reference a string
    for example).

    Now even if your code is not OO, you still have some php functions
    returning objects i.e. pg_connect, mysql_result... a connection resource
    is an object, a resultset is an object, then if you manipulate them or
    return them in some functions, return the reference, not the copy.

    another good idea to save your memory is to unset your objects once your
    done with 'em, like one would do in C++ maybe.

    .b
    odel Guest

  5. #5

    Default Re: Which is better, pass, or not pass by reference?

    Randell D.:
    >
    > Folks,
    >
    > I've asked this before but never got any response but its important and I
    > thought I'd pitch it again (hopefully a bit clearer)...
    >
    > One can pass data from one function to another either by passing a copy,
    > or passing by reference.
    >
    > My understanding of passing by reference (putting the & before a variable
    > during the function declaration) means that the original data is used.
    >
    > My understanding of *not* passing by reference, (thus passing a copy)
    > literally
    > means creating a copy of the data contained in my variables - thus
    > doubling the memory usage for this data... even if its just a temporary
    > while the function is being executed.
    >
    > I *believed* (past tense) that one should only pass by reference if they
    > wanted the child function to change the data and pass the newer values
    > back to the parent function.
    >
    > I'm now thinking that this is just a feature and not the only usage. I am
    > begining to think I should use "pass by reference" nearly all the time in
    > order to conserve memory (since I won't be creating copies of my variable
    > data in memory).
    >
    > Would this be good practice? Am I right that passing by reference means
    > that my variable data is not duplicated thus using less memory than if I
    > had passed my variable data as a copy?
    That's very risky business IMO. At some point you're bound to forget about
    it, and change a variable that was passed by reference. This can lead to
    lots of hard to catch bugs. It is generally accepted that pointers are a
    bad thing, and they should be avoided whenever you can. Functional
    programming languages go hest in this respect, and this also makes them
    easier to program.

    Also note that the PHP engine has some tricks up it's sleeve to avoid
    unnecessary copying ([url]http://www.zend.com/zend/art/ref-count.php[/url]), but I'm
    not sure if this works with argument passing as well.

    All in all I don't think it's worth the hassle. If memory is a problem you
    should instead find the memory hotspots and optimize them, possibly
    rewriting them in C.

    André Nęss
    André Nęss Guest

  6. #6

    Default Re: Which is better, pass, or not pass by reference?

    Carved in mystic runes upon the very living rock, the last words of odel
    of comp.lang.php make plain:
    > Reference is useful only for object.
    That's not true. Passing by reference is useful if you want the function to
    modify the variable you send to it.
    > (I'm not even sure you can reference a string for example).
    Of course you can.

    --
    Alan Little
    Phorm PHP Form Processor
    [url]http://www.phorm.com/[/url]
    Alan Little Guest

  7. #7

    Default Re: Which is better, pass, or not pass by reference?

    Alan Little didn't write:
    > odel wrote:
    but Alan Little wrote:
    >> (I'm not even sure you can reference a string for example).
    > Of course you can.
    You can pass all variables by reference.
    What you can't pass by reference are constants:

    <?php
    function xyz(&$p1, &$p2, &$p3) {
    // do nothing
    }

    $s = 'string';
    $i = 7;
    xyz($s, $i, 'ERROR HERE');
    ?>

    --
    I have a spam filter working.
    To mail me include "urkxvq" (with or without the quotes)
    in the subject line, or your mail will be ruthlessly discarded.
    Pedro Guest

Similar Threads

  1. pass by reference or value?
    By findapollo in forum Macromedia Flash Actionscript
    Replies: 2
    Last Post: February 19th, 10:00 AM
  2. Pass by Reference
    By hairybobby webforumsuser@macromedia.com in forum Macromedia Director 3D
    Replies: 3
    Last Post: October 6th, 12:46 PM
  3. OCIBindByName & pass-by-reference
    By Laurent Drouet in forum PHP Development
    Replies: 0
    Last Post: September 17th, 08:28 AM
  4. Replies: 0
    Last Post: September 7th, 06:53 PM
  5. #17309 [Com]: pass by reference in call_user_func
    By spagmoid at yahoo dot com in forum PHP Development
    Replies: 0
    Last Post: August 30th, 09:28 PM

Bookmarks

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139