[cvs] [Wiki] changed: Doc/Dev/Injector

Chuck Hagenbuch chuck at horde.org
Fri Jan 15 17:39:59 UTC 2010


chuck  Fri, 15 Jan 2010 12:39:59 -0500

Modified page: http://wiki.horde.org/Doc/Dev/Injector
New Revision:  1.7
Change log:  finish first pass wiki markup for gunnar's docs

@@ -242,83 +242,73 @@
  Of course this connection must be provided from somewhere. The  
application using Horde_X might simply provide it when creating the  
Horde_X instance. If the application is however using a dependency  
injection framework then this framework would be required to provide  
the required database connection.

  +++ Getting rid of singletons?

-From the viewpoint of dependency injection Horde_X can be used now as
-it allows external injection of its dependencies. We could throw away
-the singleton now. However there might be some reasons why we would
-like to keep the singleton() method. One of the reasons might be
-backward compatibility as some other classes or applications are bound
-to use the method. Another reason might be that we want to clarify how
-to get a functional instance of the class to somebody just looking at
-the Horde_X class.
+From the viewpoint of dependency injection Horde_X can be used now as  
it allows external injection of its dependencies. We could throw away  
the singleton now. However there might be some reasons why we would  
like to keep the singleton() method. One of the reasons might be  
backward compatibility as some other classes or applications are bound  
to use the method. Another reason might be that we want to clarify how  
to get a functional instance of the class to somebody just looking at  
the Horde_X class.

  We could keep the following singleton method:

-     static public function singleton()
-     {
-         if (!isset(self::$_instance)) {
-             global $conf;
-
-             $db = DB::connect($conf['sql']);
-             self::$_instance = Horde_X($db);
-         }
-
-         return self::$_instance;
-     }
+<code type="php">
+    static public function singleton()
+    {
+        if (!isset(self::$_instance)) {
+            global $conf;

+            $db = DB::connect($conf['sql']);
+            self::$_instance = Horde_X($db);
+        }

-Result
-======
+        return self::$_instance;
+    }
+</code>

-The final result that can be used with a dependency injection
-framework and still provides a backward compatible singleton method:
+The final result that can be used with a dependency injection  
framework and still provides a backward compatible singleton method:

- class Horde_X
- {
-     /**
-      * Instance object.
-      *
-      * @var Horde_X
-      */
-     static protected $_instance;
-
-     /**
-      * Pointer to a DB instance.
-      *
-      * @var DB
-      */
-     protected $_db;
-
-     /**
-      * Attempts to return a reference to a concrete Horde_X instance.
-      *
-      * @return Horde_X  The concrete Horde_X reference.
-      */
-     static public function singleton()
-     {
-         if (!isset(self::$_instance)) {
-             global $conf;
-
-             $db = DB::connect($conf['sql']);
-             self::$_instance = Horde_X($db);
-         }
-
-         return self::$_instance;
-     }
-
-     /**
-      * Constructor.
-      *
-      * @param DB $db A database connection.
-      */
-     public function __construct(DB $db)
-     {
-         $this->_db = $db;
-     }
- }
+<code type="php">
+class Horde_X
+{
+    /**
+     * Instance object.
+     *
+     * @var Horde_X
+     */
+    static protected $_instance;

+    /**
+     * Pointer to a DB instance.
+     *
+     * @var DB
+     */
+    protected $_db;

+    /**
+     * Attempts to return a reference to a concrete Horde_X instance.
+     *
+     * @return Horde_X  The concrete Horde_X reference.
+     */
+    static public function singleton()
+    {
+        if (!isset(self::$_instance)) {
+            global $conf;
+
+            $db = DB::connect($conf['sql']);
+            self::$_instance = Horde_X($db);
+        }
+
+        return self::$_instance;
+    }
+
+    /**
+     * Constructor.
+     *
+     * @param DB $db A database connection.
+     */
+    public function __construct(DB $db)
+    {
+        $this->_db = $db;
+    }
+}
+</code>


  ++ Dependency Injection Container FAQ




More information about the cvs mailing list