150

I'm using the Facebook library with this code in it:

class FacebookRestClient { ... public function &users_hasAppPermission($ext_perm, $uid=null) { return $this->call_method('facebook.users.hasAppPermission', array('ext_perm' => $ext_perm, 'uid' => $uid)); } ... } 

What does the & at the beginning of the function definition mean, and how do I go about using a library like this (in a simple example)

3 Answers 3

180

An ampersand before a function name means the function will return a reference to a variable instead of the value.

Returning by reference is useful when you want to use a function to find to which variable a reference should be bound. Do not use return-by-reference to increase performance. The engine will automatically optimize this on its own. Only return references when you have a valid technical reason to do so.

See Returning References.

Sign up to request clarification or add additional context in comments.

2 Comments

So, without going into too detail, you would call (with the class instantiated) something like $results = $facebook->users_hasAppPermission($param1, $param2); ? I guess I'm not sure of the nuance here, thanks for the help though.
Yep - I'd just call it like that.
18

It's returning a reference, as mentioned already. In PHP 4, objects were assigned by value, just like any other value. This is highly unintuitive and contrary to how most other languages works.

To get around the problem, references were used for variables that pointed to objects. In PHP 5, references are very rarely used. I'm guessing this is legacy code or code trying to preserve backwards compatibility with PHP 4.

7 Comments

It's the official Facebook PHP library, FWIW.
@Alex: In that case, they're probably doing it to protect casual hackers, who use php4, from them selves. You shouldn't do this in your own code - It's deprecated.
"This is highly unintuitive and contrary to how most other languages works" I couldn't disagree more.
@WildlyInaccurate: Assigning objects by reference by default, which is what happens in .NET and in Java (I think), is highly unintuitive. In most other languages, assignment is done by copy, whether you have an "object" or a primitive or whatever, and since this has been the case since the dawn of time, this is what the world ought to have stuck to.
Whether something is intuitive is probably rather subjective. But for languages that are otherwise similar to PHP, the norm is to pass objects by reference. In regards to OOP, I would say that is the most intuitive way, since OOP is about encapsulating state, so programs would usually refer to that state, rather than cloning it.
|
8

This is often known in PHP as Returning reference or Returning by reference.

Returning by reference is useful when you want to use a function to find to which variable a reference should be bound. Do not use return-by-reference to increase performance. The engine will automatically optimize this on its own. Only return references when you have a valid technical reason to do so.

PHP documentation on Returning reference

A reference in PHP is simply another name assigned to the content of a variable. PHP references are not like pointers in C programming, they are not actual memory addresses, so they cannot be used for pointer arithmetics.

The concept of returning references can be very confusing especially to beginners, so an example will be helpful.

$populationCount = 120; function &getPopulationCount() { global $populationCount; return $populationCount; } $countryPopulation =& getPopulationCount(); $countryPopulation++; echo "\$populationCount = $populationCount\n"; // Output: $populationCount = 121 echo "\$countryPopulation = $countryPopulation\n"; //Output: $countryPopulation = 121 

The function getPopulationCount() defined with a preceding &, returns the reference to the content or value of $populationCount. So, incrementing $countryPopulation, also increments $populationCount.

5 Comments

Why are we using & when assigning the return value? $countryPopulation =& getPopulationCount();
From my possibly wrong understanding: $countryPopulation =& getPopulationCount(); could be rewritten in English as $countryPopulation =& memoryAddressReturnedByFunction;, which can then be rewritten as $countryPopulation = associateThisMemAdrsToVariable'sValue(0xABCDABCD). $countryPopulation-->"120". On the other hand: $countryPopulation = getPopulationCount(); could be rewritten in English as $countryPopulation <-- 0xABCDABCD. $countryPopulation-->"0xABCDABCD". PHP won't show you this memory address though, as it will assume you meant =&, converting the mem addr
The & sign is used to assign by reference (address in memory) instead of assigning by value.
Won't it be enough to have the start the function with & sign?
@SherifTarek apprently not. Ommit the $ in the assignment and $populationCount stays 120. But I would type the line as $countryPopulation = &getPopulationCount();

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.