Reputation: 1816
There's lots of questions here on objects, classes, instances, instantiation etc... But I can't find the answer to my question. I'm trying to write some documentation and having trouble finding the correct terminology to use.
There's a good answer here that's related, but not what I'm looking for here.
There's also some basic info over on Wikipedia here.
Consider the following (in the PHP realm although it may be the same for other languages) :
Let's say I have singleton class:
class fooBar
{
protected static $instance;
private function __construct() {
}
public static function getInstance()
{
if(self::$instance === null) {
self::$instance = new fooBar;
}
return self::$instance;
}
}
I then instantiate the class:
$foo = fooBar::getInstance();
Later on in my code I use the getInstance
method to get a reference:
$bar = fooBar::getInstance();
So when I'm referring to $bar
, what is the correct terminology:
$bar
is a reference to the fooBar class instance.$bar
is a reference to a fooBar class instance.$bar
is the fooBar class instance.$bar
returns a reference to the fooBar class instance.$bar
returns the fooBar class instance.$bar
returns a fooBar class instance.Which of these is correct (if any) ?
Lastly, when referring to the already instantiated fooBar
class in general do I refer to it as "The fooBar instance"?
Upvotes: 1
Views: 71
Reputation: 4905
It is a variable which holds fooBar class singleton instance. Since objects in PHP are passed by reference it is the same as fooBar class singleton instance. I would say go with option 3), everybody will understand you. Options 4)-6) are simply incorrect and 1) looks overcomplicated for me.
Upvotes: 1