ballhogjoni Posted November 4, 2008 Share Posted November 4, 2008 How do you pass an object as a reference? I read the manual, but I don't understand the concept, so what does reference mean in laymens terms? IE function somefunction(&$object){} Link to comment https://forums.phpfreaks.com/topic/131387-noob-question-about-references/ Share on other sites More sharing options...
rhodesa Posted November 4, 2008 Share Posted November 4, 2008 #1) In PHP Objects are always passed by reference. For other stuff like strings, arrays, etc, you put the & in front of the variable in the argument list: function myFunc(&$value){ $value = 'This is changing the value both here and in the scrip'; } Passing by reference just means you are passing the 'pointer' or 'reference' to the variable instead of the value. Normally, if you pass a variable (not applicable to objects as they are always by reference), it only passes the value. Then, assigns that value to the proper variable in the function. That variable is local to that function and exists only for the lifetime of that function call. Link to comment https://forums.phpfreaks.com/topic/131387-noob-question-about-references/#findComment-682339 Share on other sites More sharing options...
ballhogjoni Posted November 4, 2008 Author Share Posted November 4, 2008 so basically if I were to pass an array that looks like this: $value = array("1","2","3"); and pass it in as a reference, then in my function do this: function myFunc(&$value){ $value = 'This is changing the value both here and in the scrip'; return $value; } it would output 'This is changing the value both here and in the scrip' in my script? Link to comment https://forums.phpfreaks.com/topic/131387-noob-question-about-references/#findComment-682371 Share on other sites More sharing options...
DarkWater Posted November 4, 2008 Share Posted November 4, 2008 No, you wouldn't have to return it at all. Try this code and see if you get it: <?php function noref($var) { $var = "Inside of function"; } function yesref(&$var) { $var = "Inside of function"; } $a = "Outside of function"; noref($a); echo "$a\n"; $b = "Outside of function"; yesref($b); echo "$b\n"; //Just to show you pass by reference without & in function header: noref(&$a); echo "$a\n"; ?> Link to comment https://forums.phpfreaks.com/topic/131387-noob-question-about-references/#findComment-682375 Share on other sites More sharing options...
ballhogjoni Posted November 4, 2008 Author Share Posted November 4, 2008 WOW, ok this is quite interesting! DarkWater, good example. Link to comment https://forums.phpfreaks.com/topic/131387-noob-question-about-references/#findComment-682379 Share on other sites More sharing options...
DarkWater Posted November 4, 2008 Share Posted November 4, 2008 It may seem cool at first, but you really don't want to always be using references. Sometimes its hard to track where variables are being edited if you do. It would be better to do like: <?php function noref($var) { $var = "Inside the function."; return $var; } $a = "Outside the function"; $a = noref($a); echo $a; ?> Then you can clearly see where it's being modified. Link to comment https://forums.phpfreaks.com/topic/131387-noob-question-about-references/#findComment-682383 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.