Reference and Class?
Posted: Tue Mar 18, 2003 10:37 am
I'm reading the "Singleton Pattern" tutorial on phppatterns.com and I'm having trouble wrapping my brain around some of his code. Here is the code I'm stuck on.
I think I kind of understand part of it. He declares a variable static so that it will retain it value when called again. Then it looks like he instanciates a new object, but the target object is the variable $class. Can "new" be used for something other than to create an instance variable? It appears he also makes that a reference, but it's a reference to a class. What does that do?
Just about the time I get things figured out, I find someone 3 levels above me and all the sudden I can't figure the code out even when it's commented.
Thanks in advance to all you true PHP gurus who constantly drag my skills ahead by taking time to answer my questions.
BTW, is the search feature on PHP.net broken. I get the number of matches found, but an empty list of results. Weird!
Code: Select all
// A generic function to create and fetch static objects
function staticInstance($class)
{
// Declare a static variable to hold the object instance
static $instance;
// If the instance is not there, create one
if(!isset($instance)) {
$instance =& new $class;
}
return($instance);
}Just about the time I get things figured out, I find someone 3 levels above me and all the sudden I can't figure the code out even when it's commented.
Thanks in advance to all you true PHP gurus who constantly drag my skills ahead by taking time to answer my questions.
BTW, is the search feature on PHP.net broken. I get the number of matches found, but an empty list of results. Weird!