By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,637 Members | 2,825 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,637 IT Pros & Developers. It's quick & easy.

returning references

P: n/a
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?
Jul 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a

"Justin Koivisto" <sp**@koivi.com> wrote in message
news:ik****************@news7.onvoy.net...
I was wondering if there was a way to return a reference of an object
that lies within another object...
<sample snipped>
Anyone know of a way to do this, or can you only return a copy instead
of the reference?


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;
?>

HTH
Garp
Jul 17 '05 #2

P: n/a
Garp wrote:
"Justin Koivisto" <sp**@koivi.com> wrote in message
news:ik****************@news7.onvoy.net...
I was wondering if there was a way to return a reference of an object
that lies within another object...


<sample snipped>
Anyone know of a way to do this, or can you only return a copy instead
of the reference?


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;
?>


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!
Jul 17 '05 #3

P: n/a

"Justin Koivisto" <sp**@koivi.com> wrote in message
news:sX****************@news7.onvoy.net...
Garp wrote:
"Justin Koivisto" <sp**@koivi.com> wrote in message
news:ik****************@news7.onvoy.net...
I was wondering if there was a way to return a reference of an object
that lies within another object...


<sample snipped>
Anyone know of a way to do this, or can you only return a copy instead
of the reference?


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;
?>


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!


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

Garp
Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.