RE: [PHP-DEV] Native Singleton Implementation
| From: | Peter Hodge | Date: | Tue, 06 Mar 2007 05:55:28 +0000 |
| Subject: | RE: [PHP-DEV] Native Singleton Implementation | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-28262@lists.php.net to get a copy of this message | ||
Also posted in the bug report:
Perhaps some nicer solutions to your got-cha's:
- What happens when $members = new members() is called
If the class is defined as a singleton, then PHP should create the
instance as
normal (this includes calling __construct() ) and store it in the hash
table,
or return the cached instance if it's already created.
-- I say an exception being thrown and a suggestion to remove "new"
You should still be able to use 'new', with the expectation that if the
class
is a singleton, you might actually get a cached instance, rather than a
'new'
instance, but it's not very difficult to understand:
$a = new members;
$b = new members;
$c = new members;
// $a $b and $c are all the same instance because members is a singleton
If you really think 'new' is too ambiguous, why not add a new token such as
'only', which is used for instantiation of singletons?
$a = only members;
$b = only members;
$c = only members;
// it's a bit more obvious $a $b and $c are the same instance.
- Is it ok to go $members = members; ?
That's too ambiguous. In that example 'members' could be a constant,
singleton, or simply the string 'members'.
- Singleton constructors should not have parameters
Perhaps they could (and it might be useful), if it's done like this:
$a = new members(1, 2, 3); // arguments are passed to __construct()
$b = new members(3, 2, 1); // arguments are ignored, object has already been
constructed
$c = new members(4, 5, 6); // arguments are ignored, object has already been
constructed
- Perhaps a new function singleton_getobjects needs to be added to enable
access to the internal hash table
Not a bad idea. Getting in 'through the back door' is really important. Why
not also a 'singleton_is_loaded()', and an 'is_singleton()'? Obviously this
would eventually need to be visible through the Reflection classes (etc, etc).
- Never have singleton abstract classes?
I think that would be an unfair limitation.
You should just need to:
- add T_SINGLETON to the parser.
- add an is_singleton flag for class definitions, activated by T_SINGLETON.
- overload the 'new' operator to retrieve from the hash table if the class is a
singleton (otherwise call __construct() as normal).
- implement some of those helper functions such as singleton_getobjects().
regards,
Peter
--- scott.mcnaught@synergy8.com wrote:
> Sure, implementing singleton is easy enough. But you can't abstract it in
> PHP.
>
> In C++ you can use things called template classes or generics to abstract
> the singleton implementation. This way you dont have to duplicate the
> protected constructor, the instance and the accessor for each class.
>
> I guess I'm really talking about the bigger picture here, where you might
> have an application which has around 50 singleton classes. I dont want to
> duplicate code in each one.
>
> I have already implemented singleton in two different ways (see examples 1
> and 2 in the bug report - http://bugs.php.net/bug.php?id=39946)
> and they
> both have drawbacks.
>
> Hmm... give the bug report a quick read over again :)
>
>
> -----Original Message-----
> From: Guilherme Blanco [mailto:guilhermeblanco@gmail.com]
> Sent: Monday, 5 March 2007 6:21 AM
> To: scott.mcnaught@synergy8.com
> Cc: internals@lists.php.net
> Subject: Re: [PHP-DEV] Native Singleton Implementation
>
> Hi,
>
> The idea behing a native singleton class implementation should be
> really handy, but this is fairly simple to achieve.
>
> Take a look at PHP documentation to see some patterns implementation:
> http://www.php.net/manual/en/language.oop5.patterns.php
>
>
> I hope this helps.
>
>
> Best regards,
>
> On 3/4/07, scott.mcnaught@synergy8.com <scott.mcnaught@synergy8.com> wrote:
> > *** Sorry - forgot to change the subject ***
> >
> > Hello All,
> >
> > I am new to the php internals mailing list. I am a fairly experienced
> > programmer with a few ideas floating around. I have come from a C++ games
> > development background and have now moved to primarily writing in php.
> >
> > One thing that I used extensively in C++ was the singleton design pattern.
> > I assume that most of you know what that is, if not a quick google search
> > will let you know. I have written an implementation of singleton in php,
> > but it's slow, and you have to use strings to reference the classes, or
> > duplicate a lot of code. (See methods 1 and 2 below)
> >
> > I recently submitted an RFC to bugs.php.net about this, but there was
> never
> > feedback from the internals group, so I am going to try here.
> >
> > http://bugs.php.net/bug.php?id=39946
> >
> > In a few months time, I would like to start helping the development of
> php,
> > but I am too busy at the moment.
> >
> > I was wondering if anyone has the time to implement this winner feature.
> I
> > think it would encourage good programming practise and speed up a lot of
> php
> > apps. Also I would like people's thoughts and feedback on the idea.
> >
> > If no-one can implement this before me, I will have a go at it in a few
> > months time.
> >
> > Thankyou,
> >
> > Scott McNaught
> >
> >
> >
> > Description:
> > ------------
> > ###
> > # Introduction
> > ###
> > This document is an RFC for adding a small patch to the zend engine to
> > allow for native singleton classes. The problem
> > is that currently theres no clean way to implement singleton classes in
> > user-land over the entirety of a project.
> >
> > Singleton classes are beneficial because:
> >
> > - Removes the overhead of having multiple instances of the one object
> > when there is no need
> > - Allows you to keep the objects state rather than always starting from
> > an initial state.
> > - They provide namespaces with the benefits of polymorphism (eg -
> > singleton classes can override / inherit from each other)
> >
> > Throughout this document, I will use an example of a singleton class
> > "members" which acts as an interface to a database table.
> > This class can save and load members from this database table simply by
> > calling the following methods in this class.
> >
> > members::get($member_id) Loads a member from a member id and returns an
> > associative array with info about that member
> > members::save($member) Saves a member to the database from an array of
> > properties about that member
> >
> > With the recent phase of tiered and service oriented architecture, the
> > need for Singleton has become more and more apparent.
> >
> > ###
> > # Singleton in php5
> > ###
> > In the past, I have implemented Singleton two different ways. Both of
> > these have problems.
> >
> > # Method 1:
> > The first method involves having a public static getInstance method in
> > every singleton class. This sucks because you
> > need to manually copy and paste it into every singleton class you make.
> > Using a singleton class in this way is also confusing
> > for novice programmers. Eg:
> >
> > <?php
> >
> > class members
> > {
> > static public function getInstance()
> > {
> > static $object = null;
> >
> > if($object)
> > {
> > return $object;
> > }
> >
> > $object = new members();
> > return $object;
> > }
> >
> > /**
> > * Returns a member from the database based on their id
> > */
> > function get($id)
> > {
> > // ...
> > }
> >
> > // save method etc...
> > }
> >
> > // Usage
> > $arrMember = members::getInstance()->get(49);
> > $arrMember['member_f_name'] = 'Scott';
> > members::getInstance()->save($arrMember);
> >
> > ?>
> >
> > # Method 2:
> > This method involves an associative array of class names to their
> > instances, probably via a helper function similar to this.
> >
> > <?php
> >
> > class members
> > {
> > /**
> > * Returns a member from the database based on their id
> > */
> > function get($id)
> > {
> > // ...
> > }
> >
> > // save method etc...
> > }
> >
> > /**
> > * Returns an instance of a singleton class from its class name
> > */
> > function getInstance($strClass)
> > {
> > static $objects = array();
> >
> > if(!isset($objects[$strClass]))
> > {
> > return $objects[$strClass];
> > }
> >
> > $objects[$strClass] = new members();
> > return $objects[$strClass];
> > }
> >
> > // Usage
> > $arrMember = getInstance('members')->get(49);
> > $arrMember['member_f_name'] = 'Scott';
> > getInstance('members')->save($arrMember);
> >
> > ?>
> >
> > This sucks because its slow, confusing for novices, and IDEs never pick
> > up the class for code hinting.
> >
> > ###
> > # Proposed new functionality
> > ###
> >
> > I propose that singleton classes become a native part of the php
> > language by adding the "singleton" class modifier T_SINGLETON.
> >
> > I don't know if native singleton classes have been implemented in a
> > language before. Most other languages eg - C++
> > you can use template classes or generics to implement a clean
> > singleton.
> >
> > The zend engine could feature a hash table and store the instances of
> > objects.
> >
> > An example of the new way of using singleton classes would be:
> >
> > <?php
> >
> > /**
> > * A class for saving / retreiving members from a database
> > */
> > singleton class members extends dataadapter
> > {
> > /**
> > * Returns a member from the database based on their id
> > */
> > function get($id)
> > {
> > // ...
> > }
> >
> > // save method etc...
> > }
> >
> > // Usage
> > $arrMember = members->get(49);
> > members->save($arrMember);
> >
> > ?>
> >
> > ###
> > # Edge cases / got-cha's
> > ###
> > Some investigation will have to be performed as to how inheritence will
> > work with singletons.
> >
> > Eg
> > - Never have singleton abstract classes?
> >
> > - What happens when $members = new members() is called
> > -- I say an exception being thrown and a suggestion to remove "new"
> >
> > - Is it ok to go $members = members; ?
> > -- I'd say yes
> >
> > - Singleton constructors should not have parameters
> >
> > - Perhaps a new function singleton_getobjects needs to be added to
> > enable access to the internal hash table
> >
> > - Dereferencing a non-singleton class should produce the same error
> >
> > --
> > PHP Internals - PHP Runtime Development Mailing List
> > To unsubscribe, visit: http://www.php.net/unsub.php
> >
> >
>
>
> --
> Guilherme Blanco - Web Developer
> CBC - Certified Bindows Consultant
> Cell Phone: +55 (16) 9166-6902
> MSN: guilhermeblanco@hotmail.com
> URL: http://blog.bisna.com
> São Carlos - SP/Brazil
>
> --
> PHP Internals - PHP Runtime Development Mailing List
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
Send instant messages to your online friends http://au.messenger.yahoo.com