/    Sign up×
Community /Pin to ProfileBookmark

OOP in PHP5 Question…What’s the purpose of "&" in with parameters in methods?

Every now and then I’m seeing

class foo {

function foo1( [COLOR=”Red”]&[/COLOR]$x ) {

}

}

what’s the purpose of this with parameters?

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@eval_BadCode_Feb 06.2011 — invoke the method and pass the argument by reference.

try it:
[code=php]
<?php
class foo {
public static function bar( &$x ) { ++$x; }
public function baz( &$x ) { ++$x; }

public static function notByRef( $x ) { ++$x; }

}


$x = 9;

echo $x, "<br />";

foo::bar($x);

echo $x, "<br />";

$cthulu = new foo();
$cthulu->baz($x);

echo $x, "<br />";

foo::notByRef($x);

echo $x, "<br />";

?>
[/code]




It has nothing to do with OOP or PHP5.

Its a long story, but basically you can create "privacy leaks" by passing things by reference in OOP, that's about all it has to do with OOP.
Copy linkTweet thisAlerts:
@NogDogFeb 06.2011 — Not really an OOP question, actually. ? It means that the parameter is "passed by reference", so any changes made to that variable within the function will be applied to the variable passed to it within the calling scope:
[code=php]
function foo($var) // normal pass by copy
{
$var = $var * 2;
}
function bar(&$var) // pass by reference
{
$var = $var * 2;
}
$test = 3;
foo($test);
echo $test; // "3" -- no change in this scope
bar($test);
echo $test; // "6" -- change applied in this scope as well
[/code]
Copy linkTweet thisAlerts:
@ChuckBauthorFeb 06.2011 — got it...thanks guys...
Copy linkTweet thisAlerts:
@ChuckBauthorMar 13.2011 — @ NogDog...

I was just reviewing this thread posted a little while ago, and if you remember when I was trying to create a universal RSS code by creating a class/object, and the issue I was having was the same photo was appearing because it wasn't iterating through the feed items, because the incremented value wasn't being read...

well, it looks like using '&' is the solution...because if you remember I was trying to increment a argument passed to a method...

[code=php]class foo{

"property 1"
"property 2"
"property 3"

public function show($count){

++$count;

}

}[/code]


which should've been..

[code=php]public function show(&$count){

++$count;

}[/code]



hopefully I'm making sense :o, but it seems based on this thread that the only way to increment an argument that's passed to a method and use it to iterate through an array of values is to use '&'...

this is what I'm seeing from eval(BadCode)'s example...his example printed out

[code=php]
9
10
11
11
[/code]


and the method for the last value didn't use '&'
Copy linkTweet thisAlerts:
@NogDogMar 13.2011 — Without understanding the bigger picture, I'm hesitant to say that's the "best" solution. It has the "smell" of coupling the class too closely to the application code. What if you want to re-use the class in a situation where you do not want it to increment the supplied argument? Why should the class care whether or not the client code's variable is incremented automatically? Therefore, under the concept of separation of responsibility, for this simple case I would probably favor returning the incremented value, and then letting the calling code decide whether or not to apply it to the argument by using that returned value.
[code=php]
class Foo
{
public function bar($var)
{
return ++$var;
}
}
[/code]

Calling code:
[code=php]
$foo = new Foo();
$var = 1;
$var = $foo->bar($var);
// or if you don't want $var itself to change:
$var_2 = $foo->($var);
[/code]
×

Success!

Help @ChuckB spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...