Re: OO and "Singleton" class
| From: | Paul M Foster | Date: | Wed, 02 Sep 2009 04:45:00 +0000 |
| Subject: | Re: OO and "Singleton" class | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-297595@lists.php.net to get a copy of this message | ||
On Tue, Sep 01, 2009 at 06:06:24PM -0400, Eddie Drapkin wrote:
> On Tue, Sep 1, 2009 at 5:16 PM, Paul M Foster<paulf@quillandmouse.com> wrote:
> >
> > I'm a little fuzzy on some of the PHP implementation details for some
> > stuff. In PHP (5 <= phpversion() < 5.3), I'd like a configuration class
> > which can only effectively be instantiated once. Will the following code
> > do this? Any other suggestions or corrections? (Code is untested. Feel
> > free to complain about syntax.)
<snip>
>
> The primary thing you need to do to make a class a singleton is
> disallow the instantiation of a class directly. Which means using
> visibility, which is conspicuously absent from your example above.
>
> So, to prevent normal instantiation, we make the constructor private.
> Also, we need a method to retrieve the instance. Static members serve
> this purpose:
Good explanation. Thanks. I'll try it out.
Paul
--
Paul M. Foster