Lukas Smith wrote:
> So please explain to me how you would write a singleton static method
> for a base class from which you can inherite. Suddenly the idea with
> using the class name becomes less useful.

IMHO, you need singleton factory :)

Inheritance is very strong relationship between classes. For example I
have base class 'Animal' and derived classes 'Dog' and 'Cat'. I can find
similarity between 'Dog' and 'Cat'. (They are animals)

If I have base class Singleton and derived classes 'MyCar' and 'MyCat'.
I can't find similarity between this classes.

Solutions is:

class SingletonFactory {
        private $instances;

        __construct() {
                $this->instances = array();
        }

        function getInstance($name) {
                if(is_a($this->instances[$name], $name) === false) {
                        $this->instances[$name] = new $name;
                }
                
                return $this->instances[$name];
        }
}

FYI: SingletonFactory can be a pure static class.

-- 
Ondrej Ivanic
([EMAIL PROTECTED])

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to