NYCPHP Meetup

NYPHP.org

[nycphp-talk] How to create a singleton class in PHP

jon baer jonbaer at jonbaer.net
Thu Feb 12 12:10:07 EST 2004


i have not had to use it much but this is the function i have played around
with when i first tried oo-php:

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

which i got from a good website:
http://www.phppatterns.com/index.php/article/articleprint/6/-1/1/

i think the lack of static variables makes it impossible - something php5
will provide.

- jon




More information about the talk mailing list