3

Possible Duplicate:
What does it mean to start a PHP function with an ampersand?

Recently I stumbled upon this piece of code:

public static function &get_instance()
{
return self::$instance;
}

What does this kind of function declaration &get_instance() mean? Can the function name be a variable?

Community
  • 1
  • 1
gopi1410
  • 6,567
  • 9
  • 41
  • 75
  • 5
    it's just bad and deprecated code. – OZ_ Jun 08 '12 at 19:51
  • anyways what does it mean? & its compatibility? – gopi1410 Jun 08 '12 at 19:52
  • @OZ_ btw, found it while exploring `codeigniter` – gopi1410 Jun 08 '12 at 19:52
  • 3
    it's passing result by reference - deprecated. – OZ_ Jun 08 '12 at 19:52
  • ohk, i see. If so, then its of no use.. – gopi1410 Jun 08 '12 at 19:54
  • @gopi1410: It's more damage than useful. Looks like a singleton to me. – Madara's Ghost Jun 08 '12 at 19:55
  • @Truth: what exactly is a singleton? – gopi1410 Jun 08 '12 at 19:57
  • Yeah, I noticed that and edited my comment. There is something on my screen so it looked like a - in front of the vote count. Stupid monitor. – WhoaItsAFactorial Jun 08 '12 at 19:57
  • 1
    @gopi1410: A Singleton, putting it short, is forcing an object to have a single instance. And that instance will be available globally across the application. This has multiple disadvantages, and little advantages. It is considered harmful. – Madara's Ghost Jun 08 '12 at 20:04
  • @Truth: In web-based PHP code it's only really harmful in the sense that it makes testing harder. Any PHP code is only "global" for a single http request. – Scott Saunders Jun 08 '12 at 20:11
  • Possible duplicate of [What does it mean to start a PHP function with an ampersand?](http://stackoverflow.com/questions/1676897/what-does-it-mean-to-start-a-php-function-with-an-ampersand) also please see [Reference - What does this symbol mean in PHP?](http://stackoverflow.com/questions/3737139/reference-what-does-this-symbol-mean-in-php) --- @OZ_: That's not deprecated. But anyway, you should not use this, hopefully "codeigniter" implies this for the TS. – hakre Jun 09 '12 at 16:05

2 Answers2

4

It's part of the singleton pattern, in old-style code.

Singleton is a pattern used to make sure that there is only one instance of a class. (Technically it can be used to make sure that there are a certain number of instances of any class, but that number is almost always one.) It's one of the Gang of Four patterns and you can find endless discussion of its use and abuse all over the web.

Scott Saunders
  • 29,840
  • 14
  • 57
  • 64
  • hm.. but what exactly is a singleton pattern? – gopi1410 Jun 08 '12 at 19:58
  • See [Singleton pattern](http://en.wikipedia.org/wiki/Singleton_pattern) – Brad Koch Jun 08 '12 at 20:34
  • Possible duplicate of [What does it mean to start a PHP function with an ampersand?](http://stackoverflow.com/questions/1676897/what-does-it-mean-to-start-a-php-function-with-an-ampersand) also please see [Reference - What does this symbol mean in PHP?](http://stackoverflow.com/questions/3737139/reference-what-does-this-symbol-mean-in-php) – hakre Jun 09 '12 at 16:06
3

It means that the result of get_instance() is being returned by reference. Since objects are always by reference since PHP 5, it doesn't make sense to write code like that anymore. Incidentally, the public is also particularly curious, since that means this is PHP 5 code.

Brad Koch
  • 19,267
  • 19
  • 110
  • 137