functions return by reference question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts

  • John Downey
    Guest replied
    Re: functions return by reference question

    Fabian Wleklinski wrote:[color=blue]
    > May be. That's what would happen in other languages. But I don't
    > know if PHP really copies the result two times: 1.) from a local
    > variable inside the method into the result (inside the stack as well),
    > 2.) from the result into the assigned variable. May be PHP goes
    > another way. I haven't checked the php-source, so I cannot assume
    > this behaviour.[/color]
    Well i am going off what I was told when I used to idle in #php on
    irc.freenode.ne t

    --
    John Downey




    Leave a comment:


  • Fabian Wleklinski
    Guest replied
    Re: functions return by reference question

    Hi John, Hi Lawrence,
    [color=blue]
    > Well PHP wouldn't return a reference[/color]

    Right.
    [color=blue]
    > what he posted would work what it would do is tell PHP to assign
    > $allEntries to point to the memory address of whatever
    > array_reverse() was returning instead of copying the data like it
    > would normally do.[/color]

    May be. That's what would happen in other languages. But I don't
    know if PHP really copies the result two times: 1.) from a local
    variable inside the method into the result (inside the stack as well),
    2.) from the result into the assigned variable. May be PHP goes
    another way. I haven't checked the php-source, so I cannot assume
    this behaviour.
    [color=blue]
    > In PHP you can pass arround Objects and
    > Arrays as refernces but passing them like array_reverse(& $allEntries);
    > is no longer needed or allowed unless you turn off the option in php.ini.[/color]

    Yes, PHP throws a warning if you do so. But Lawarence hasn't :-)

    Greetings from Frankfurt / Germany,

    Fabian Wleklinski


    Leave a comment:


  • John Downey
    Guest replied
    Re: functions return by reference question

    Fabian Wleklinski wrote:
    [color=blue]
    > Hi lawrence,
    >
    >[color=green]
    >>if I go:
    >>
    >>$allEntries = & array_reverse($ allEntries);[/color]
    >
    >
    > No, I don't think so, becauce array_reverse does not return
    > a reference, as far as I know.
    >
    > Greetings from Frankfurt / Germany,
    >
    > Fabian Wleklinski
    >
    >[/color]
    Well PHP wouldn't return a reference what he posted would work what it
    would do is tell PHP to assign $allEntries to point to the memory
    address of whatever array_reverse() was returning instead of copying the
    data like it would normally do. In PHP you can pass arround Objects and
    Arrays as refernces but passing them like array_reverse(& $allEntries);
    is no longer needed or allowed unless you turn off the option in php.ini.

    --
    John Downey




    Leave a comment:


  • Fabian Wleklinski
    Guest replied
    Re: functions return by reference question

    Hi lawrence,
    [color=blue]
    > if I go:
    >
    > $allEntries = & array_reverse($ allEntries);[/color]

    No, I don't think so, becauce array_reverse does not return
    a reference, as far as I know.

    Greetings from Frankfurt / Germany,

    Fabian Wleklinski


    Leave a comment:


  • lawrence
    Guest replied
    Re: functions return by reference question

    "Fabian Wleklinski" <Wleklinski.NNT P@eWorks.de> wrote in message[color=blue][color=green]
    > > I get back a reference to the object automatically now?[/color]
    >
    > Thats alright. But you have to use the &-operator a second
    > time when calling the function:
    >
    > $referencedObje ct =& myCoolFunction( );
    >
    > (Yes. Really. It is ugly, but it is true. See [1].)[/color]


    if I go:

    $allEntries = & array_reverse($ allEntries);

    Then the array is returned by reference? PHP's memory usage is not
    doubled in this exchange? At no point are copies made?

    I need to do what I can to keep the memory to a minimum.

    Leave a comment:


  • Fabian Wleklinski
    Guest replied
    Re: functions return by reference question

    Hi Lawrence,
    [color=blue]
    > If I want a function to return by reference, I do this?
    >
    > function & myCoolFunction( ) {
    > $queryObject = new queryObject();
    > return $queryObject;
    > }
    >
    >
    > I get back a reference to the object automatically now?[/color]

    Thats alright. But you have to use the &-operator a second
    time when calling the function:

    $referencedObje ct =& myCoolFunction( );

    (Yes. Really. It is ugly, but it is true. See [1].)


    Greetings from Frankfurt/Germany,

    Fabian Wleklinski


    [1] https://www.php.net/manual/en/functi...ing-values.php


    Leave a comment:


  • lawrence
    Guest started a topic functions return by reference question

    functions return by reference question

    If I want a function to return by reference, I do this?

    function & myCoolFunction( ) {
    $queryObject = new queryObject();
    return $queryObject;
    }


    I get back a reference to the object automatically now?
Working...