Ding container: Using the Xml, Yaml, and Annotations drivers
TweetUsing XML, Yaml, or Annotations in your PHP Applications for Dependency Injection
In "Installing and using the Ding DI Container" I've shown how to download and install the ding container. The article ended with getting a container instance, but didn't show how to actually define and use your beans. So that's what this article is all about.
So ding is basically about beans (aka components). The ding manual contains a little more information about beans and about the whole container. What you will find here is kind of a "hands-on" approach to it.
The idea here is to put your code in your components (beans) following the Single Responsibility Principle, and let the container take care of the rest (like injecting dependencies or maybe even choosing which dependencies to inject and where). So lots of your actual real code will become declarative stuff, that the container interprets and acts upon. As a result, your code will end up containing only the lines of code that are the true value of the application. Also, your code will become more testable, because you will no longer need to programatically get your dependencies.
So that's how it works: code your components. Then, define (declare) your dependencies to the container. When your components are needed, they will be injected with any dependencies they might need.
Declaring your beans
Ding supports 3 bean providers: Xml, Yaml, and Annotations. You can use 1 or all 3 of them, you need at least 1 but you can mix them in any way you need. The Xml and Yaml files are non code-invasive. In contrast, using annotations is kind of invasive, because you need to actually annotate the code, but they are a little more agile in the daily developer's work. The most used combination is to have a very short xml or yaml file and the rest using annotations throughout the code. Let's see an example of a bean declared in the 3 drivers (just a preview), then I'll show how to configure them in the container:
And the equivalent Yaml:
And the equivalent using annotations:
Configuring the 3 drivers to get your components or beans from the PHP Container
The drivers are specified in the container configuration options:
In this case we are configuring all the drivers:
- xml: We are defining 2 possible names for the bean files, beans.xml and otherbeans.xml. Possible candidates are the directory where the script is, and the subdirectory "otherDir". So ding will try to find these files in these directories.
- yaml: Same as xml, but with .yaml files.
- annotation: We're declaring the directory where the script is as one of the directories to be scanned for annotations.
Enough to get you started and write Great PHP Applications
To get right into dependency injection, continue with this article. Also, You can find more about the xml and yaml drivers here:
- Ding manual
- All ding examples at Github: Including yaml, xml, aspects, setter/constructor injection, using arrays, properties, etc.