Reference and Class?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
EricS
Forum Contributor
Posts: 183
Joined: Thu Jul 11, 2002 12:02 am
Location: Atlanta, Ga

Reference and Class?

Post 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!
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

Phuse is best for searching!

http://use.devnetwork.net/
bionicdonkey
Forum Contributor
Posts: 132
Joined: Fri Jan 31, 2003 2:28 am
Location: Sydney, Australia
Contact:

Post 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
Post Reply