returning references

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

    returning references

    I was wondering if there was a way to return a reference of an object
    that lies within another object...

    <?php
    class my_class_1{
    var $_db=NULL;

    function my_class_1(){
    $this->_db=new my_class_2(TRUE );
    return TRUE;
    }

    function get_db(&$db){
    $db=&$this->db;
    return TRUE;
    }
    }

    class my_class_2{
    var $var1=NULL;

    function my_class_2($var ){
    $this->var1=$var;
    return TRUE;
    }
    }

    $object=new my_class_1();
    $object->get_db($db);
    var_dump($db);
    ?>

    This gives:
    NULL

    I was hoping for:
    object(my_class _2)(1) {
    ["var1"]=>
    bool(true)
    }

    Anyone know of a way to do this, or can you only return a copy instead
    of the reference?
  • Garp

    #2
    Re: returning references


    "Justin Koivisto" <spam@koivi.com > wrote in message
    news:ikjdc.432$ m3.16865@news7. onvoy.net...[color=blue]
    > I was wondering if there was a way to return a reference of an object
    > that lies within another object...
    >[/color]
    <sample snipped>[color=blue]
    >
    > Anyone know of a way to do this, or can you only return a copy instead
    > of the reference?[/color]



    Returning References
    Returning by-reference is useful when you want to use a function to find
    which variable a reference should be bound to. When returning references,
    use this syntax:


    <?php
    function &find_var ($param)
    {
    /* ...code... */
    return $found_var;
    }

    $foo =& find_var ($bar);
    $foo->x = 2;
    ?>

    HTH
    Garp


    Comment

    • Justin Koivisto

      #3
      Re: returning references

      Garp wrote:
      [color=blue]
      > "Justin Koivisto" <spam@koivi.com > wrote in message
      > news:ikjdc.432$ m3.16865@news7. onvoy.net...
      >[color=green]
      >>I was wondering if there was a way to return a reference of an object
      >>that lies within another object...[/color]
      >
      > <sample snipped>
      >[color=green]
      >>Anyone know of a way to do this, or can you only return a copy instead
      >>of the reference?[/color]
      >
      > http://uk.php.net/manual/en/language...ces.return.php
      >
      > Returning References
      > Returning by-reference is useful when you want to use a function to find
      > which variable a reference should be bound to. When returning references,
      > use this syntax:
      >
      > <?php
      > function &find_var ($param)
      > {
      > /* ...code... */
      > return $found_var;
      > }
      >
      > $foo =& find_var ($bar);
      > $foo->x = 2;
      > ?>[/color]

      Now isn't that just hallarious! I had read that page 5 times and missed
      the & before the function name... (no wonder I kept getting warnings,
      errors and notices)

      Damn long days.... I'm goin' to the pub now. ;) Enjoy!

      THANKS!

      Comment

      • Garp

        #4
        Re: returning references


        "Justin Koivisto" <spam@koivi.com > wrote in message
        news:sXjdc.435$ m3.16903@news7. onvoy.net...[color=blue]
        > Garp wrote:
        >[color=green]
        > > "Justin Koivisto" <spam@koivi.com > wrote in message
        > > news:ikjdc.432$ m3.16865@news7. onvoy.net...
        > >[color=darkred]
        > >>I was wondering if there was a way to return a reference of an object
        > >>that lies within another object...[/color]
        > >
        > > <sample snipped>
        > >[color=darkred]
        > >>Anyone know of a way to do this, or can you only return a copy instead
        > >>of the reference?[/color]
        > >
        > > http://uk.php.net/manual/en/language...ces.return.php
        > >
        > > Returning References
        > > Returning by-reference is useful when you want to use a function to find
        > > which variable a reference should be bound to. When returning[/color][/color]
        references,[color=blue][color=green]
        > > use this syntax:
        > >
        > > <?php
        > > function &find_var ($param)
        > > {
        > > /* ...code... */
        > > return $found_var;
        > > }
        > >
        > > $foo =& find_var ($bar);
        > > $foo->x = 2;
        > > ?>[/color]
        >
        > Now isn't that just hallarious! I had read that page 5 times and missed
        > the & before the function name... (no wonder I kept getting warnings,
        > errors and notices)
        >
        > Damn long days.... I'm goin' to the pub now. ;) Enjoy!
        >
        > THANKS![/color]

        It's not the world's greatest bit of syntax. 8) Glad to help.

        Garp


        Comment

        Working...