Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:28320 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 45247 invoked by uid 1010); 11 Mar 2007 02:30:06 -0000 Delivered-To: ezmlm-scan-internals@lists.php.net Delivered-To: ezmlm-internals@lists.php.net Received: (qmail 45232 invoked from network); 11 Mar 2007 02:30:06 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 11 Mar 2007 02:30:06 -0000 Authentication-Results: pb1.pair.com header.from=scott.mcnaught@synergy8.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=scott.mcnaught@synergy8.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain synergy8.com designates 202.174.102.10 as permitted sender) X-PHP-List-Original-Sender: scott.mcnaught@synergy8.com X-Host-Fingerprint: 202.174.102.10 hosting.synergy8.com Received: from [202.174.102.10] ([202.174.102.10:36969] helo=hosting.synergy8.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 76/49-40270-8A963F54 for ; Sat, 10 Mar 2007 21:30:03 -0500 Received: from synergy8 by hosting.synergy8.com with local (Exim 4.52) id 1HQDos-0007Nl-LG; Sun, 11 Mar 2007 12:29:58 +1000 Received: from 127.0.0.1 ([127.0.0.1]) (SquirrelMail authenticated user scott.mcnaught@synergy8.com) by synergy8.com with HTTP; Sun, 11 Mar 2007 12:29:58 +1000 (EST) Message-ID: <36961.127.0.0.1.1173580198.squirrel@synergy8.com> Date: Sun, 11 Mar 2007 12:29:58 +1000 (EST) To: "Tony Marston" Cc: internals@lists.php.net User-Agent: SquirrelMail/1.4.6 MIME-Version: 1.0 Content-Type: text/plain;charset=iso-8859-1 Content-Transfer-Encoding: 8bit X-Priority: 3 (Normal) Importance: Normal X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - hosting.synergy8.com X-AntiAbuse: Original Domain - lists.php.net X-AntiAbuse: Originator/Caller UID/GID - [32007 501] / [47 12] X-AntiAbuse: Sender Address Domain - synergy8.com X-Source: /usr/local/cpanel/3rdparty/bin/php X-Source-Args: /usr/local/cpanel/3rdparty/bin/php /usr/local/cpanel/base/3rdparty/squirrelmail/src/compose.php X-Source-Dir: :/base/3rdparty/squirrelmail/src Subject: Re: [PHP-DEV] Native Singleton Implementation From: scott.mcnaught@synergy8.com > > wrote in message > news:001101c7630e$0cad1470$0900a8c0@scottnote... >> Yeah that's the same way that I implement it. You're still using >> strings >> for referencing class names. > > So what? Is there an alternative method? Does it have any advantages? Yes! Moving the singleton functionality into core php. Its an excellent OOP feature. > >> Sorry my file didn't get attached when sent to the list. >> http://hlrally.net/media/singletonabc.inc >> >> The advantages are: >> - Speed improvements > > Where is your proof? Think about it. You won't be calling upon php code every time you want just to return an instance of a class. I'm sure APC (and others) could run the usage code faster because they are given the ability to store a class name rather than strings. > >> - Allows for context sensitive code hinting in IDEs > > How is this of benefit? ... ! ... you must use a text editor such as vi or notepad... (not that theres anything wrong with that) > >> - Simplicity > > I disagree. My method is simpler and more intuitive than yours, and does > not > need any changes in core PHP. The only difference between my method (http://hlrally.net/media/singletonabc.inc) and yours is that - You have a local static variable for your table of instances - You dont inherit from the singleton class - You dont enforce a protected constructor (so multiple instances could be created by noobs) - You have some lame hack to translate the names of 3 of your classes to files - You allow for an argument to be passed to your constructor (usually considered bad practise with singletons anyway) When I said simplicity, I was meaning using the native singleton method, its easy for the programmer. They dont need to write their own singleton management class (or research them on tutorial sites). And the usage (accessing the singleton) can be made simpler as well. > >> - Encourages good coding practices for amateur programmers > > I disagree. What is "good" to you is "not good" to others. There is more > than one way of implementing the singleton pattern, (or any design pattern > for that matter) so trying to enforce one particular method is just too > short sighted for my taste. You can always still implement the variations using php. All Im proposing is a simple mechanism for implementing single instance classes. I think everyone needs to use this. > > -- > Tony Marston > http://www.tonymarston.net > http://www.radicore.org > >> >> -----Original Message----- >> From: Tony Marston [mailto:tony@marston-home.demon.co.uk] >> Sent: Saturday, 10 March 2007 9:55 PM >> To: internals@lists.php.net >> Subject: Re: [PHP-DEV] Native Singleton Implementation >> >> >> wrote in message >> news:001a01c762b8$805d6f60$0900a8c0@scottnote... >>> Hi Tony, >>> >>> I do have a single singleton class to deal with singletons (as you put >>> it). >>> I have attached it to this email for your reference. >>> >>> But it's back to my original problem where you either have to: >>> - Duplicate code for each singleton class ... or ... >>> - Reference your classes via strings (slower, stops code hinting from >>> working etc) >> >> I see no such problem with my implementation of a single class for >> singletons. >> >>> I use method two as you do and it is not nice. There is no nice way of >>> implementing singleton in php. >> >> I disagree. Take a look at >> http://www.tonymarston.net/php-mysql/singleton.html#global.helper.with.loade >> r >> >>> Have a full read of >>> http://bugs.php.net/bug.php?id=39946 >>> >>> Also, with three tiered architecture, there is even more reason to use >>> singleton. You have yet another layer, filled with classes that need >>> only >>> be >>> instantiated once. I think as a developer that uses tiered >>> architecture, >>> you would benefit greatly from this feature. >> >> I already have a more-than-adequate method of dealing with singletons >> (see >> http://www.tonymarston.net/php-mysql/singleton.html#global.helper.with.loade >> r) >> and I see absolutely no advantage in the method which you propose. >> Putting >> this into core PHP would be of no benefit to anyone. >> >> -- >> Tony Marston >> http://www.tonymarston.net >> http://www.radicore.org >> >>> Scott >>> >>> -----Original Message----- >>> From: Tony Marston [mailto:tony@marston-home.demon.co.uk] >>> Sent: Friday, 9 March 2007 11:39 PM >>> To: internals@lists.php.net >>> Subject: Re: [PHP-DEV] Native Singleton Implementation >>> >>> >>> wrote in message >>> news:001a01c75ffd$fa352520$0900a8c0@scottnote... >>>> Hi Greg, >>>> >>>> There is no reason why you can't still use the current method to >>>> implement >>>> your variations to the singleton design pattern. But, yes, I am >>>> proposing >>>> that the strict "text book" variety is implemented. >>> >>> There is no strict "text book" implementation of the singleton pattern. >>> There are different implementations in different languages, so it is >>> not >>> possible to decide that one implementation is "pure" while all the >>> others >>> are "impure". My personal choice is to avoid a singleton method withn >>> each >>> class and have a single singleton class instead, thus avoiding a lot of >>> code >>> >>> duplication. >>> >>>> If you haven't had the need to singleton-ize more than 2-3 classes, >>>> you >>>> probably haven't worked with tiered architecture, or haven't thought >>>> to >>>> use >>>> singleton in this situation. >>> >>> I use a single singleton class which can instaniate any number of >>> database >>> table objects in my business layer, but I only have a single object in >>> my >>> data access layer. This object is instantiated from a class which is >>> specific to the DBMS being used (MySQL, PstreSQL, or Oracle). >>> >>>> The way I work is with two-tiered architecture. I have a presentation >>>> tier >>>> which consists of HTML and php which renders the HTML. I then have a >>>> second >>>> tier which is a data access tier. >>> >>> The 3 Tier Architecture is better - presentation, business (domain) and >>> data >>> >>> access - as it provides for a beter separation of responsibiities. >>> >>>> In the data access tier, I have one class >>>> for each database table. In these classes there are SQL statements >>>> for >>>> saving, retrieving records, in fact any operation that I do to the >>>> table. >>>> No SQL resides outside of the table's data access class. Each one of >>>> these >>>> classes is a singleton. >>>> >>>> There are two things that a native singleton implementation would >>>> solve: >>>> >>>> - At the data access layer, it is nasty to duplicate the singleton >>>> pattern >>>> for each class, (or worse, work with strings for class names). >>>> >>>> - From the presentation layer, it is ugly to work with the data access >>>> singletons. >>>> >>>> Example: A page in the presentation tier which lists all news posts >>>> >>>> getAll() as $post){ >>>> ?> >>>> >>>>

>>>>

>>>> >>>> >>>> >>>> It would be nicer (and much more efficient) to be able to go: >>>> >>>> getAll() as $post){ ?> >>>> >>>>

>>>>

>>>> >>>> >>>> >>>> >>>> >>>> I actually have about 50 singleton classes in my current project, and >>>> trust >>>> me, they are all warranted. >>> >>> If you are complaining that your 50 singleton classes produces too much >>> duplicated code, then your implementation is wrong. If you have a >>> single >>> class to deal wth singletons then there is no code duplication. >>> >>> I do not think it is a good idea to implement a new "feature" into core >>> PHP >>> which serves no useful purpose other than to disguise inefficent coding >>> practices. >>> >>> -- >>> Tony Marston >>> http://www.tonymarston.net >>> http://www.radicore.org >>> >>>> I have put a copy of a screen shot of my >>>> eclipse project at: >>>> http://hlrally.net/media/content_pages/gotsingleton.png >>>> I put a blue line next to the singleton classes. The folder >>>> "components" >>>> represents the data access layer. The presentation layer is not seen >>>> in >>>> that shot. >>>> >>>> Now we all have different programming styles and use different >>>> architectural >>>> principles to each other. Just remember this is not a debate about >>>> what >>>> architecture is better etc. I am just showing an example. >>>> >>>> Scott >>>> >>>> -----Original Message----- >>>> From: Gregory Beaver [mailto:greg@chiaraquartet.net] >>>> Sent: Tuesday, 6 March 2007 4:12 PM >>>> To: toomuchphp-phpdev@yahoo.com >>>> Cc: scott.mcnaught@synergy8.com; 'Guilherme Blanco'; >>>> internals@lists.php.net >>>> Subject: Re: [PHP-DEV] Native Singleton Implementation >>>> >>>> Peter Hodge wrote: >>>>> 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(). >>>> >>>> Hi, >>>> >>>> This seems both to be excessively magical and inflexible. For >>>> instance, >>>> I have implemented global singletons, singleton per-configuration path >>>> (i.e. for a configuration object with cascading configuration files, >>>> one >>>> singleton per configuration file location), and other models for >>>> singleton that stray on the continuum between singleton and factory. >>>> The proposed implementation allows only the textbook singleton. >>>> >>>> The benefit of having singleton in the zend engine in my book is zero. >>>> If you have 10 gazillion classes that are all singletons, perhaps >>>> another look at the design is in order? I've never had to >>>> singleton-ize >>>> more than 2-3 classes in even the most complex projects I've been >>>> involved with. >>>> >>>> Greg >>>> >>>> -- >>>> PHP Internals - PHP Runtime Development Mailing List >>>> To unsubscribe, visit: http://www.php.net/unsub.php >>> >>> -- >>> PHP Internals - PHP Runtime Development Mailing List >>> To unsubscribe, visit: http://www.php.net/unsub.php >> >> -- >> PHP Internals - PHP Runtime Development Mailing List >> To unsubscribe, visit: http://www.php.net/unsub.php > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > >