Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:28223 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 51159 invoked by uid 1010); 4 Mar 2007 17:57:05 -0000 Delivered-To: ezmlm-scan-internals@lists.php.net Delivered-To: ezmlm-internals@lists.php.net Received: (qmail 51144 invoked from network); 4 Mar 2007 17:57:05 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 4 Mar 2007 17:57:05 -0000 Authentication-Results: pb1.pair.com smtp.mail=scott.mcnaught@synergy8.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=scott.mcnaught@synergy8.com; 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:41011] helo=hosting.synergy8.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 0B/30-46646-F680BE54 for ; Sun, 04 Mar 2007 12:57:04 -0500 Received: from dsl-202-173-148-163.qld.westnet.com.au ([202.173.148.163] helo=scottnote) by hosting.synergy8.com with esmtpa (Exim 4.52) id 1HNux8-0006Q3-VN for internals@lists.php.net; Mon, 05 Mar 2007 03:56:59 +1000 Reply-To: To: References: <45E33990.8030806@adaniels.nl> <1331353216.20070226232944@marcus-boerger.de> <45E37D05.5040000@adaniels.nl> <408821909.20070303232323@marcus-boerger.de> <1508893792.20070304144812@marcus-boerger.de> <592868326.20070304163838@marcus-boerger.de> <26646995.20070304183706@marcus-boerger.de> Date: Mon, 5 Mar 2007 03:56:55 +1000 Message-ID: <001e01c75e86$8043bce0$0900a8c0@scottnote> MIME-Version: 1.0 Content-Type: text/plain; charset="US-ASCII" Content-Transfer-Encoding: 7bit X-Mailer: Microsoft Office Outlook 11 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3028 In-Reply-To: <26646995.20070304183706@marcus-boerger.de> Thread-Index: Acdeg70eLHhituP6Rx+g9YSJDQHt1gAAK74g 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 - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - synergy8.com X-Source: X-Source-Args: X-Source-Dir: Subject: RE: [PHP-DEV] suggestion SplFileInfo From: scott.mcnaught@synergy8.com 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: 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. 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: 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