Hi Richard,

the problem is this:

<?php
abstract class singleton
{
    static protected $_instance;
    
        static final public function getInstance()
        {
            $caller = get_called_class();
            if (!static::$_instance instanceof $caller) {
                echo "NEW INSTANCE\n";
            static::$_instance = new $caller;
            }

            return static::$_instance;
        }
}

class foo extends singleton {
        static protected $_instance;
}

class bar extends singleton {
        static protected $_instance;
}

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

NEW INSTANCE
object(foo)#1 (0) {
}
NEW INSTANCE
object(bar)#2 (0) {
}
object(foo)#1 (0) {
}
object(bar)#2 (0) {
}

This means i just called once the constructor for the singleton abstract for 
every main class (foo, bar)
But if you remove the static member $_instance from the foo and bar class you 
get this result:

NEW INSTANCE
object(foo)#1 (0) {
}
NEW INSTANCE
object(bar)#2 (0) {
}
NEW INSTANCE
object(foo)#1 (0) {
}
NEW INSTANCE
object(bar)#2 (0) {
}

You see that the constructor is called every time if you use more than 1 
singleton class during script execution.
The problem is that the property isnt really inherited by the foo and bar class.

My idea or a possible solution would be to allow the definition of correct 
inheritance of the $_instance property or
to show me a way to set a static property dynamicly in the caller class.
Maybe Andi or Markus can explain this a bit more why this isnt possible. :)

-- Marco

> -----Original Message-----
> From: Richard Quadling [mailto:[EMAIL PROTECTED]
> Sent: Friday, November 23, 2007 10:22 AM
> To: Marco Kaiser
> Cc: PHP Developers Mailing List
> Subject: Re: [PHP-DEV] late static binding php6
> 
> On 23/11/2007, Richard Quadling <[EMAIL PROTECTED]> wrote:
> > On 22/11/2007, Marco Kaiser <[EMAIL PROTECTED]> wrote:
> > > 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
> > abstract 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 {
> > }
> >
> > class bar extends singleton {
> > }
> >
> > var_dump(foo::getInstance());
> > var_dump(bar::getInstance());
> > var_dump(foo::getInstance());
> > var_dump(bar::getInstance());
> > ?>
> >
> > returns ...
> >
> > object(foo)#1 (0) {
> > }
> > object(bar)#2 (0) {
> > }
> > object(foo)#1 (0) {
> > }
> > object(bar)#2 (0) {
> > }
> >
> > in PHP 5.3.0-dev (cli) (built: Nov 20 2007 08:19:12)
> >
> > I think this is great! Well done everyone. Unless I've completely
> > missed the point.
> 
> Also, you can make the getInstance() method final so it cannot be
> overridden in sub-classes...
> 
> <?php
> abstract class singleton
> {
>        static protected $_instance = null;
>        static final public function getInstance()
>        {
>            $caller = get_called_class();
>            if (!static::$_instance instanceof $caller) {
>            static::$_instance = new $caller;
>            }
> 
>            return static::$_instance;
>        }
> }
> 
> class foo extends singleton {
>       public function getInstance() { return 'foobar'; }
> }
> 
> class bar extends singleton {
> }
> 
> var_dump(foo::getInstance());
> var_dump(bar::getInstance());
> var_dump(foo::getInstance());
> var_dump(bar::getInstance());
> ?>
> 
> returns ...
> 
> Fatal error: Cannot override final method singleton::getInstance() in
> C:\sing.php on line 18
> 
> So, a very useful addition to the language.
> 
> --
> -----
> Richard Quadling
> Zend Certified Engineer :
> http://zend.com/zce.php?c=ZEND002498&r=213474731
> "Standing on the shoulders of some very clever giants!"


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

Reply via email to