thiswayup
thiswayup

Reputation: 2077

what does "&" mean in front of classes in php?

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.

Upvotes: 2

Views: 1325

Answers (5)

Alana Storm
Alana Storm

Reputation: 166086

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.

Upvotes: 5

Eric Petroelje
Eric Petroelje

Reputation: 60508

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.

Upvotes: 2

Jonathan Fingland
Jonathan Fingland

Reputation: 57167

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 https://www.php.net/manual/en/language.references.php for more information on references in php in general

Upvotes: 0

Randolpho
Randolpho

Reputation: 56391

& 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.

Upvotes: 1

Andrew Hare
Andrew Hare

Reputation: 351536

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.

Upvotes: 4

Related Questions