views:

574

answers:

2

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)

+8  A: 

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.

Dominic Rodger
In addition, http://php.net/references might help out a bit also.
Stephen Caldwell
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.
Alex Mcp
Yep - I'd just call it like that.
Dominic Rodger
+1  A: 

It's returning a reference, as mentioned already. In PHP4, 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 PHP5, references are very rarely used. I'm guessing this is legacy code or code trying to preserve backwards compatibility with php4.

troelskn
It's the official Facebook PHP library, FWIW.
Alex Mcp
Info on the new PHP 5 Object Model (as opposed to the old pass-by-value nonsense (and other nonsense) from PHP4): http://php.net/manual/en/migration5.oop.php
Dereleased
@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.
troelskn