Hi again,

to explain the main idea a bit more, the code below work and moves the
main getInstance function from the class and its possible to abstract
this.
it would be cool to get the protected property also into the abstract
class. Any idea or maybe a solution in the near future?

<?php
abstract class singleton
{
        static public function getInstance()
        {
            $caller = get_called_class();
            if (!static::$_instance instanceof $caller) {
            static::$_instance = new $caller;
            }

            return static::$_instance;
        }
}

class foo extends singleton {
        static protected $_instance = null;
}

class bar extends singleton {
        static protected $_instance = null;
}

var_dump(foo::getInstance());
var_dump(bar::getInstance());
var_dump(foo::getInstance());
var_dump(bar::getInstance());
?>

On Nov 22, 2007 9:29 PM, Marco Kaiser <[EMAIL PROTECTED]> wrote:
> Hi List,
>
> just to drop my note here, i asked (i think) 2 years ago for such a
> feature to automate my singleton pattern. Not with late static
> bindings this is possible.
>
> <?php
> class singleton
> {
>         static protected $_instance = null;
>
>         static public function getInstance()
>         {
>             $caller = get_called_class();
>             if (!static::$_instance instanceof $caller) {
>             static::$_instance = new $caller;
>             }
>
>             return static::$_instance;
>         }
> }
>
> class foo extends singleton
> {
> }
>
> var_dump(foo::getInstance());
> var_dump(foo::getInstance());
> ?>
>
> i think this will also drop much redundant code from some frameworks. :)
> So this is one of my examples that helps much.
>
>
> --
> Marco Kaiser
>



-- 
Marco Kaiser

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

Reply via email to