neoform Posted April 12, 2008 Share Posted April 12, 2008 I've run into a problem which I don't think can be solved since php only has references and does not have pointers.. $GLOBALS['glob_var'] = 10; funk($var); echo 'SHOULD BE 10: '.$var.'<br />\n'; function funk(&$ref_var) { $ref_var = &$GLOBALS['glob_var']; echo 'SHOULD BE 10: '.$ref_var.'<br />\n'; } This outputs: SHOULD BE 10: 10 SHOULD BE 10: Any ideas on how to fix this without duplicating the contents of $GLOBALS['glob_var'] ? Quote Link to comment https://forums.phpfreaks.com/topic/100802-phps-lack-of-pointers/ Share on other sites More sharing options...
hellbringer Posted April 12, 2008 Share Posted April 12, 2008 Use $GLOBALS without '&'. Quote Link to comment https://forums.phpfreaks.com/topic/100802-phps-lack-of-pointers/#findComment-515507 Share on other sites More sharing options...
neoform Posted April 12, 2008 Author Share Posted April 12, 2008 Wouldn't that make a copy of the variable and not assign it to the reference? Quote Link to comment https://forums.phpfreaks.com/topic/100802-phps-lack-of-pointers/#findComment-515517 Share on other sites More sharing options...
papaface Posted April 12, 2008 Share Posted April 12, 2008 What are you trying to do? Quote Link to comment https://forums.phpfreaks.com/topic/100802-phps-lack-of-pointers/#findComment-515522 Share on other sites More sharing options...
alecks Posted April 12, 2008 Share Posted April 12, 2008 with global-ized variables or super globals like $GLOBALS it isn't necessary to reference them within a function, ex $my_var = 12345; echo $my_var; function afunc() { global $my_var; $my_var = 23456; } afunc(); echo '<br />'.$my_var; would output 12345 23456 References are useful when passing variables to functions and you want to modify them, ex: $my_var = 12345; echo $my_var; function afunc(&$ref_to_my_var) { $ref_to_my_var = 23456; } afunc($my_var); echo '<br />'.$my_var; will output 12345 23456 Quote Link to comment https://forums.phpfreaks.com/topic/100802-phps-lack-of-pointers/#findComment-515527 Share on other sites More sharing options...
neoform Posted April 12, 2008 Author Share Posted April 12, 2008 I'm using this purely as an example, my real code involves class member variables and they aren't integers, they're objects.. i just used this example to simplify it. realistically, i want to pass a reference to the object without duplicating it (which would use up a lot more memory). Maybe I wasn't clear enough before, I'm looking to pass back a reference.. this should work, but doesn't. $GLOBALS['glob_var'] = 10; funk($var); echo 'SHOULD BE 10: '.$var."<br />\n"; $GLOBALS['glob_var'] = 11; echo 'SHOULD BE 11: '.$var."<br />\n"; function funk(&$ref_var) { $ref_var = &$GLOBALS['glob_var']; echo 'SHOULD BE 10: '.$ref_var."<br />\n"; } Quote Link to comment https://forums.phpfreaks.com/topic/100802-phps-lack-of-pointers/#findComment-515529 Share on other sites More sharing options...
alecks Posted April 12, 2008 Share Posted April 12, 2008 $GLOBALS['glob_var'] = 10; $var =& funk(); echo 'SHOULD BE 10: '.$var."<br />\n"; $GLOBALS['glob_var'] = 11; echo 'SHOULD BE 11: '.$var."<br />\n"; function funk() { $ref_var = &$GLOBALS['glob_var']; echo 'SHOULD BE 10: '.$ref_var."<br />\n"; return $ref_var } ??? Quote Link to comment https://forums.phpfreaks.com/topic/100802-phps-lack-of-pointers/#findComment-515535 Share on other sites More sharing options...
neoform Posted April 12, 2008 Author Share Posted April 12, 2008 That's more along the line of what I'm trying to do, but this still gives a copy of the var, not a reference $GLOBALS['glob_var'] = 10; $var = & funk(); echo 'SHOULD BE 10: '.$var."<br />\n"; $GLOBALS['glob_var'] = 11; echo 'SHOULD BE 11: '.$var."<br />\n"; function funk() { return $GLOBALS['glob_var']; } SHOULD BE 10: 10 SHOULD BE 11: 10 Quote Link to comment https://forums.phpfreaks.com/topic/100802-phps-lack-of-pointers/#findComment-515540 Share on other sites More sharing options...
discomatt Posted April 12, 2008 Share Posted April 12, 2008 <?php $glob_var = 10; funk($var); echo 'SHOULD BE 10: '.$var."<br />\n"; function funk(&$ref_var) { $ref_var &= $GLOBALS['glob_var']; echo 'SHOULD BE 10: '.$ref_var."<br />\n"; } ?> Try that BTW - need double quotes to parse \n as linebreak. Quote Link to comment https://forums.phpfreaks.com/topic/100802-phps-lack-of-pointers/#findComment-515542 Share on other sites More sharing options...
neoform Posted April 12, 2008 Author Share Posted April 12, 2008 I'm not totally sure what $ref_var &= $GLOBALS['glob_var']; does, but my output is: SHOULD BE 10: 0 SHOULD BE 10: 0 Quote Link to comment https://forums.phpfreaks.com/topic/100802-phps-lack-of-pointers/#findComment-515554 Share on other sites More sharing options...
rhodesa Posted April 14, 2008 Share Posted April 14, 2008 Objects are passed by reference Quote Link to comment https://forums.phpfreaks.com/topic/100802-phps-lack-of-pointers/#findComment-517064 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.