[commits] [Wiki] changed: Doc/Dev/ConversionH6
Ralf Lang (B1 Systems GmbH)
lang at b1-systems.de
Thu Nov 12 06:44:19 UTC 2020
rlang Thu, 12 Nov 2020 06:44:19 +0000
Modified page: https://wiki.horde.org/Doc/Dev/ConversionH6
New Revision: 4
Change log: Use ::class as suggested by Jan
@@ -157,11 +157,38 @@
* Constructors SHOULD depend on interfaces or base classes, not on
the (single) implementation.
* HordeBase or App configuration SHOULD be depended on as *TODO*
object, not as array and explicitly not via global state
* Apps must not care for configuration of other apps, only theirs or
HordeBase
-* Namespaced app-internal objects which implement an interface should
be registered with the injector using the fully qualified name to
allow for autowiring of other code using it. e.g. if you have an
interface or abstract class Horde\Kronolith\Calendar\Resource, bind it
as 'Horde\Kronolith\Calendar\Resource' - not
'\Horde\Kronolith\Calendar\Resource\'
-* As this makes for very long keys which are hard to read and write,
as a convention, also register the same object as 'Calendar/Resource'.
This is the string you would use when calling the injector yourself
-* If an object can be autowired by the injector (all constructor
dependencies are injectable and already registered with the injector),
only register the shorthand but not the fully qualified name.
+* Namespaced app-internal objects which implement an interface should
be registered with the injector using the class combined with ::class,
e.g.
+
+<code type="php">
+ // Use a factory to get an implementation
+ // In case the factory cannot be autowired itself, you need to
register how to get it first.
+
$injector->bindFactory(Horde\Kronolith\Calendar\Resource::class,
Horde\Kronolith\Calendar\Factory::class, 'create')
+ // In case you want a specific implementation and it can be
autowired, just bind it.
+ $injector->bindImplementation(Horde\Kronolith\Renderer::class,
Horde\Kronolith\Renderer\Default::class);
+</code>
+
+* Note there are no strings, this is the class itself - ::class
returns the fully qualified strings
+* If you have imported the class with use, you can just use the
imported name:
+
+<code type="php">
+ use Horde\Kronolith\Calendar\Resource;
+ use Horde\Kronolith\Calendar\Factory as CalendarFactory;
+ use Horde\Kronolith\Renderer;
+ use Horde\Kronolith\Renderer\Default as RendererDefault;
+ // Use a factory to get an implementation
+ // In case the factory cannot be autowired itself, you need to
register how to get it first.
+ $injector->bindFactory(Resource::class,
CalendarFactory::class, 'create')
+ // In case you want a specific implementation and it can be
autowired, just bind it.
+ $injector->bindImplementation(Renderer::class,
RendererDefault::class);
+</code>
+
+
+* Registering an implementation by an arbitrary string is still
legal, we even might need it for bc compat with some designs.
+<code type="php">
+ $injector->bindImplementation('Forms', '\Horde_Forms_Base');
+</code>
+++ Optional
* Constructors SHOULD better depend on interfaces rather than base
classes but SHOULD NOT depend on the (single) implementation.
More information about the commits
mailing list