Page 1 of 1

Reference and Class?

Posted: Tue Mar 18, 2003 10:37 am
by EricS
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.

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);
}
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!

Posted: Tue Mar 18, 2003 10:42 am
by m3mn0n
Phuse is best for searching!

http://use.devnetwork.net/

Posted: Tue Mar 18, 2003 3:07 pm
by bionicdonkey
I found the best way to learn about class & object is to read through Chapter 14. Classes and Objects in the PHP Manual. it should explain all you need to know