Sunday 11 November 2018

oop - What does it mean to start a PHP function with an ampersand?



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)


Answer



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.


No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...