tags:

views:

315

answers:

5

I've just inherited this code in PHP which seems to do some kind of web service call to googles api. I'm not quite an expert on PHP and there is a few syntax questions I have specifically relating to the following line $soapClients = &APIlityClients::getClients();

I understand the double "::" as indicating that APIlityClients is a static class but I'm not sure what the "&" in front of APIlityClients means.

+3  A: 

It is PHP's version of getting a reference to something rather than copying its value. So in this case the & would retrieve a reference to the return value of APIlityClients::getClients() rather than a copy of the return value itself.

Andrew Hare
+1  A: 

& indicates a pass by reference rather than by value. It doesn't apply much in PHP5 since classes are passed by reference by default in that version, but weren't in previous versions.

Randolpho
you can, however, also return by reference as is the case in the OP question. see the link in my answer.
Jonathan Fingland
A: 

the & means it's a reference. References are used to allow multiple variables point to the same object.

this page explains it better than I can though

Also see http://us3.php.net/manual/en/language.references.php for more information on references in php in general

Jonathan Fingland
+1  A: 

It means "address of" - and it's referring to the value returned from the getClients() call, not the APllityClients class itself.

Basicly it's saying to assign $soapClients to a reference to whatever is returned from getClients() rather than making a copy of the returned value.

Eric Petroelje
+2  A: 

When you use an ampersand in front of a variable in PHP, you're creating a reference to that variable.

$foo = 'bar';
$baz = &$foo;

echo $foo //bar
echo $baz //bar

$foo = 'foobazbar';
echo $foo //foobazbar
echo $baz //foobazbar

Prior to PHP5, when you created an object from a class in PHP, that object would be passed into other variables by value. The object was NOT a reference, as is standard in most other object oriented (Java, C#, etc.) languages.

However, by instantiating a class with an ampersand in front of it, you could create a reference to the returned object, and it would behave like an object in other languages. This was a common technique prior to PHP5 to achieve OOP like effects and/or improve performance.

Alan Storm