28

Consider:

$smarty =& SESmarty::getInstance();

What is the & for?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
JasonDavis
  • 48,204
  • 100
  • 318
  • 537
  • 2
    If you need something like this in the future search for "operators" in the PHP help references, or a good search engine. – Mark Cooper Jan 17 '10 at 18:06
  • 1
    You should also look up something called "Singleton". – cwallenpoole Jan 17 '10 at 19:18
  • `=&` is not "a thing" -- it is two things that should not be smooshed together. See the explanation in this post for the reasons that you should not be writing `=&`: https://stackoverflow.com/a/63914758/2943403 – mickmackusa Sep 16 '20 at 09:42

3 Answers3

41

It passes by reference. Meaning that it won't create a copy of the value passed.

See: http://php.net/manual/en/language.references.php (See Adam's Answer)

Usually, if you pass something like this:

$a = 5;
$b = $a;
$b = 3;

echo $a; // 5
echo $b; // 3

The original variable ($a) won't be modified if you change the second variable ($b) . If you pass by reference:

$a = 5;
$b =& $a;
$b = 3;

echo $a; // 3
echo $b; // 3

The original is changed as well.

Which is useless when passing around objects, because they will be passed by reference by default.

Tyler Carter
  • 60,743
  • 20
  • 130
  • 150
  • 6
    PHP5 objects are *not* passed by reference by default: rather, an object identifier is passed by value (similar to Java's 'reference' semantics); see the manual for details: http://www.php.net/manual/en/language.oop5.references.php – Christoph Jan 17 '10 at 17:18
  • It really is kind of pointless. The main reason people used them was to not require a function to return a variable, but instead just have to modify the variable. – Tyler Carter Jan 17 '10 at 17:28
  • 2
    @Chacha102 and @jasondavis References are ABSOLUTELY not pointless. They are invaluable. Perhaps they are not always useful when working with primitives, but they are incredibly useful when working with ANY OOP -- including basic arrays. – cwallenpoole Jan 17 '10 at 19:09
  • Oh, and they make the Singleton POSSIBLE. – cwallenpoole Jan 17 '10 at 19:17
  • In addition to what Chacha102 said, from the manual: > References in PHP are a means to access the same variable content by different names. The manual section is at http://php.net/manual/en/language.references.php – Adam Hopkinson Jan 17 '10 at 17:09
8

In PHP 4, it kind of (awkwardly) associated two variables.

$j = 'original';
$i =& $j;
$i = 'modified';
echo $j; // The output is 'modified'

Likewise...

$j = 'original';
$i =& $j;
$j = 'modified';
echo $i; // The output is 'modified'

Some of this was made a little less unpleasant when it comes to objects in PHP 5, but I think the heart of it is the same, so these examples should still be valid.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Richard JP Le Guen
  • 28,364
  • 7
  • 89
  • 119
1

References are used to alias variables and were necessary to use the old object system efficiently.

In PHP 4, objects behaved like any other value type, that is, assignment would create a copy of the object. If you wanted to avoid this, you had to use a reference as in your example code.

With PHP 5, object variables no longer contain the object itself, but a handle (AKA object identifier) and assignment will only copy the handle. Using a reference is no longer necessary.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Christoph
  • 164,997
  • 36
  • 182
  • 240