note 57316 added to language.oop5.patterns
| From: | rashkatsa at osu1 dot php dot net | Date: | Fri, 30 Sep 2005 12:03:12 +0000 |
| Subject: | note 57316 added to language.oop5.patterns | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-96045@lists.php.net to get a copy of this message | ||
Hi there,
As Singleton patterns are used for years in Java, today we tend toward overriding 'new'
operator (as singleton returns always the same instance for every 'new' that you call...).
In order to do this, we use IoC (http://www.martinfowler.com/articles/injection.html). With
lightweight container, you delegate the 'new' to a specific factory which decides if it
has to return a new instance on each call or always the same instance. You homogenize your code as
you don't have "new MyClass()" and "MyClass2::getInstance()" but always the
same call : Component::get(MyClass) and Component::get(MyClass2). [PHP implementation hint : You can
choose the parameter of the get method : class, classname, ...]. It's the component which
decides if it has to return a new instance or not.
If you take a look at Java Spring framework (which use getBean(Class) method) or PicoContainer
(which use getComponentInstance(Class) method - see 2 minutes tutorial on their site), you can
configure the behavior for your class in an XML file (choosing that ClassA is a singleton and ClassB
is not). With this configuration, you don't need to pollute your class code with _instance
field but you just create a standard class (non invasive pattern).
when you would have a PHP generic component (class) factory, you can go further and build a complete
PHP IoC (if it not already exists :)) and then the component factory injects dependencies itself
(choose through constructors or mutators).
regards,
rashkatsa.
----
Manual Page -- http://www.php.net/manual/en/language.oop5.patterns.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+57316
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+57316&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+57316&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+57316&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+57316&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+57316&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+57316&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+57316&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+57316&report=yes
Search -- http://master.php.net/manage/user-notes.php