-
Notifications
You must be signed in to change notification settings - Fork 1
Dice
Garrett W edited this page Aug 30, 2021
·
5 revisions
Level-2/Dice does not support PSR-11 natively (author's choice) but there's still plenty to learn here.
- Small library size
- Auto-wiring
- Dynamic (not compiled)
- Pass custom constructor params at create-time to main function
- Configure custom constructor params per class
- Substitute objects of one class for typehints of a different (yet compatible) class/interface
- "Named instances" (allows for multiple instances of one class, such as when they are configured differently)
- Rule inheritance with subclasses
- Post-instantiation method calls which can return either the original object or the method's return value
- Share a given instance across the entire container or only with a subset of the object tree
- Lazy instances
- Fast and small; achieved via memoization and a reduced amount of ideal OOP
- Strictly convention-over-configuration, but everything should be configurable
- Minimal developer interaction should be needed
- Should be easy to learn
- Can be configured in pure PHP alone
- Decidedly NOT a service locator
- Intends to replace 99% of factories
- Will never support configuration in annotations
- Loosely follows the X.org design principles; specifically, "Do not add new functionality unless an implementor cannot complete a real application without it."
- Defaults to creating new objects each time one is required rather than serving up an existing one, but this can be changed for specific instances
- Passing the DIC object itself into classes is not recommended or specifically supported but can be done
- Custom parameters can be passed to constructors via the configuration or even in the main method call
- Can specify whether rules apply only to an exact class or also to its subclasses
- Can define substitutions of one classname for another, such as in the case of typehints being interfaces and an object of some implementing class should be supplied; this is implemented confusingly alongside an "instanceOf" property which does something a little different. Substitutions apply to constructor parameter typehints, and are typically configured for a specific object tree rather than the entire container (but the latter can also be done)
- Can call methods on objects after their creation, even fluently if desired (when chaining method calls, the return value is not the initial object created but rather the result of the final method call in the chain)
- Can share an instance of a certain class only within part of the object tree if desired
- Can define "named instances" identified by a string that is not a valid classname (The use case for this is when you need multiple instances of a different class in the container and each needs to be configured differently. The "instanceOf" property is used here to give Dice the actual classname behind a "named instance".)
- Can update rules after they are initially defined, as opposed to overwriting them
- Rules are inherited by default (if there is a rule that applies to A and one that applies to B, and B is a subclass of A, both rules will be applied when creating a B (with B's rule taking precedence if there is a conflict)
- Can reference container's own instance of a given class in any configuration rule
- Small size and speed come at the expense of readability and good coding practices. It's a trade-off and there truly isn't a way to fully accomplish both goals at once. Dice accomplishes some impressive speed numbers with some unusual, hard-to-follow techniques.
- Feature set is limited in some ways, probably intentionally, but some features from other DICs could be nice to have.
- I have found a limited set of circumstances in which the DIC object might actually need to be passed into objects and can't be avoided. So this could be a useful feature to build in.
- As it stands, Dice can't truly replace all factories -- and it would probably be useful to have some factory functionality built in so that it might can come closer to achieving that goal.